-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathall_zeros_end.cpp
More file actions
41 lines (35 loc) · 976 Bytes
/
all_zeros_end.cpp
File metadata and controls
41 lines (35 loc) · 976 Bytes
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
//
// Created by Mayank Parasar on 2019-12-17.
//
/*Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of
* the non-zero elements.
*/
#include <iostream>
#include <vector>
using namespace std;
vector<int> put_all_zero_at_end(vector<int> vect) {
// vector<int> vec;
vector<int>::iterator itr;
int orignal_size = vect.size();
for(int i = 0; i < vect.size(); i++) {
if(vect[i] == 0) {
// erase the element
itr = vect.begin() + i;
vect.erase(itr);
}
}
// do the post processing to add '0's at the end of the vector
int num_zeros = orignal_size - vect.size();
for(int i = 0; i < num_zeros; i++) {
vect.push_back(0);
};
return(vect);
}
int main() {
vector<int> vec = {0,1,0,3,12, 0};
vector<int> vec2 = put_all_zero_at_end(vec);
for(auto i: vec2)
cout << i << " ";
cout << endl;
return 0;
}