#C4582. Rearrange Array - Move All Zeros to the End

    ID: 48136 Type: Default 1000ms 256MiB

Rearrange Array - Move All Zeros to the End

Rearrange Array - Move All Zeros to the End

You are given an array of integers. Your task is to rearrange the array so that all zeros are moved to the end while keeping the relative order of the non-zero elements unchanged.

For example, if the input array is:

[ [0, 1, 0, 3, 12, 0] ]

After rearrangement, it should become:

[ [1, 3, 12, 0, 0, 0] ]

Note: The solution should maintain the stability of the non-zero elements. Use an in-place algorithm with O(n) complexity.

Hint: Use two pointers to solve the problem. One pointer traverses the array and the other one tracks the position for the next non-zero element.

inputFormat

The first line contains an integer N, representing the number of elements in the array. The second line contains N space-separated integers.

outputFormat

Output the rearranged array as space-separated integers on a single line.## sample

6
0 1 0 3 12 0
1 3 12 0 0 0

</p>