#include <bits/stdc++.h>
using namespace std;
vector<int> moveZeros(int n, vector<int> a) {
int j = -1;
//place the pointer j:
for (int i = 0; i < n; i++) {
if (a[i] == 0) {
j = i;
break;
}
}
//no non-zero elements:
if (j == -1) return a;
//Move the pointers i and j
//and swap accordingly:
for (int i = j + 1; i < n; i++) {
if (a[i] != 0) {
swap(a[i], a[j]);
j++;
}
}
return a;
}
int main()
{
vector<int> arr = {1, 0, 2, 3, 2, 0, 0, 4, 5, 1};
int n = 10;
vector<int> ans = moveZeros(n, arr);
for (auto &it : ans) {
cout << it << " ";
}
cout << '\n';
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp2ZWN0b3I8aW50PiBtb3ZlWmVyb3MoaW50IG4sIHZlY3RvcjxpbnQ+IGEpIHsKICAgIGludCBqID0gLTE7CiAgICAvL3BsYWNlIHRoZSBwb2ludGVyIGo6CiAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgIGlmIChhW2ldID09IDApIHsKICAgICAgICAgICAgaiA9IGk7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgIH0KICAgIH0KCiAgICAvL25vIG5vbi16ZXJvIGVsZW1lbnRzOgogICAgaWYgKGogPT0gLTEpIHJldHVybiBhOwoKICAgIC8vTW92ZSB0aGUgcG9pbnRlcnMgaSBhbmQgagogICAgLy9hbmQgc3dhcCBhY2NvcmRpbmdseToKICAgIGZvciAoaW50IGkgPSBqICsgMTsgaSA8IG47IGkrKykgewogICAgICAgIGlmIChhW2ldICE9IDApIHsKICAgICAgICAgICAgc3dhcChhW2ldLCBhW2pdKTsKICAgICAgICAgICAgaisrOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiBhOwp9CgoKaW50IG1haW4oKQp7CiAgICB2ZWN0b3I8aW50PiBhcnIgPSB7MSwgMCwgMiwgMywgMiwgMCwgMCwgNCwgNSwgMX07CiAgICBpbnQgbiA9IDEwOwogICAgdmVjdG9yPGludD4gYW5zID0gbW92ZVplcm9zKG4sIGFycik7CiAgICBmb3IgKGF1dG8gJml0IDogYW5zKSB7CiAgICAgICAgY291dCA8PCBpdCA8PCAiICI7CiAgICB9CiAgICBjb3V0IDw8ICdcbic7CiAgICByZXR1cm4gMDsKfQo=