-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD2_zeroToLast.cpp
More file actions
45 lines (37 loc) · 1.13 KB
/
D2_zeroToLast.cpp
File metadata and controls
45 lines (37 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Given an array arr[]. Push all the zeros of the given array to the right end of the array while maintaining the order of non-zero elements. Do the mentioned change in the array in place.
// Examples:
// Input: arr[] = [1, 2, 0, 4, 3, 0, 5, 0]
// Output: [1, 2, 4, 3, 5, 0, 0, 0]
// Explanation: There are three 0s that are moved to the end.
// Input: arr[] = [10, 20, 30]
// Output: [10, 20, 30]
// Explanation: No change in array as there are no 0s.
// Input: arr[] = [0, 0]
// Output: [0, 0]
// Explanation: No change in array as there are all 0s.
// Constraints:
// 1 ≤ arr.size() ≤ 105
// 0 ≤ arr[i] ≤ 105
#include <bits/stdc++.h>
#include <vector>
using namespace std;
void moveZerosToEnd(vector<int>& arr) {
int n = arr.size();
for (int i = 0; i < n; ++i) {
if (arr[i] != 0) {
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
j++;
}
}
}
int main() {
vector<int> arr = {1, 2, 0, 4, 3, 0, 5, 0};
moveZerosToEnd(arr);
for (int num : arr) {
cout << num << " ";
}
cout << endl;
return 0;
}