Move Zeroes

Practice this coding question on Coding Gurukul.

Problem statement

A sensor log uses zero for missing readings. Compact the log so all recorded nonzero readings keep their original relative order and missing readings appear at the end. Perform the rearrangement in place using constant extra space.

Input format

First line: n. Second line: n space-separated integers.

Output format

Print the rearranged sequence on one line.

Constraints

1 <= n <= 100000; -10000 <= each value <= 10000. Use 64-bit integers for sums.

Sample input

6
0 6 0 -2 8 0

Sample output

6 -2 8 0 0 0

Sign in to run or submit a solution in C++, Java or Python.