55. Move Zeros to the End
Core1000 ms256 MBSolved by 0%
Move every zero to the end of the array while keeping the relative order of all the non-zero values unchanged.
Print the resulting array, separated by single spaces.
For [0, 1, 0, 3, 12] the answer is 1 3 12 0 0, the non-zeros stay in the same order
they appeared.
Constraints - `1 ≤ n ≤ 200000` - `-1000000 ≤ a[i] ≤ 1000000`
Input
The first line contains an integer n.
The second line contains n space-separated integers.
Output
Print the rearranged array on ONE line, separated by single spaces.
Input5
0 1 0 3 12
Output1 3 12 0 0
Noteis the worked example from the statement
Input4
1 2 3 4
Output1 2 3 4
Notehas no zeros, so nothing moves
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.
Hint 1Where to start
Hint 2The approachOpen hint 1 first. This one carries on from it.
Hint 3PseudocodeOpen hint 2 first. This one carries on from it.
Hint 4Full solutionOpen hint 3 first. This one carries on from it.