#C2708. Move Zeros to the End

    ID: 46054 Type: Default 1000ms 256MiB

Move Zeros to the End

Move Zeros to the End

Problem Statement:

Given an array of integers, your task is to move all the zeros in the array to the end while maintaining the relative order of the non-zero elements. This operation must be done in-place with a constant extra space complexity.

Example:

Input:  [0, 1, 0, 3, 12]
Output: [1, 3, 12, 0, 0]

Note: Try to achieve an optimal time complexity of \(O(n)\) and an extra space complexity of \(O(1)\).

inputFormat

Input Format:

The first line of input contains an integer \(n\), representing the number of elements in the array. The second line contains \(n\) space-separated integers.

Example:

5
0 1 0 3 12

outputFormat

Output Format:

Output the modified array as a single line of space-separated integers with all the zeros moved to the end while preserving the relative order of non-zero elements.

Example:

1 3 12 0 0
## sample
5
0 1 0 3 12
1 3 12 0 0