Problem 15
Move Zeroes
Easy · 10 pts · Array, Two Pointers
Problem
You are given an array of N integers. Move every 0 to the end of the array while keeping all non-zero elements in the same relative order as before. Print the resulting array. A simple in-place method: walk through the array with a write position, copy each non-zero element to the write position and advance it, then fill the remaining positions with zeros.
Input
First line: integer N. Second line: N space-separated integers.
Output
Print the modified array as space-separated integers.
Constraints
1 <= N <= 100000; -1000000000 <= array[i] <= 1000000000
Example input
6 0 1 0 3 12 0
Example output
1 3 12 0 0 0
Explanation
The array is 0 1 0 3 12 0. The non-zero values in their original order are 1 3 12, and there are three zeros, so the result is 1 3 12 0 0 0.