← All coding problems
Move Zeroes
easyArrays & Hashing+10 XPTime limit 2s
Move every 0 in the array to the end, keeping the relative order of the non-zero numbers.
Input:
- line 1:
n - line 2:
nintegers
Output: the rearranged array on one line.
Constraints: 1 ≤ n ≤ 10⁵, −10⁹ ≤ a[i] ≤ 10⁹.
Challenge: do it in place with two pointers — one reading, one writing — without building a second array.
Sample input 1
5 0 1 0 3 12
Sample output 1
1 3 12 0 0
Sample input 2
1 0
Sample output 2
0
📖 Stuck? Learn the pattern
Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).