#K95362. Move Zeros to the End
Move Zeros to the End
Move Zeros to the End
Given an array of integers, your task is to move all zeros to the end of the array while maintaining the relative order of the non-zero elements. This operation should be performed in-place.
Note: The input is provided through stdin
and the output should be printed to stdout
. To accommodate multiple scenarios, the first line of the input contains an integer T representing the number of test cases. For each test case, the first line contains an integer n indicating the number of elements in the array followed by a line with n space-separated integers.
Your solution should process every test case and print the resulting array (after moving all zeros to the end) in a single line for each test case, where the numbers are space-separated.
inputFormat
The first line contains an integer T
, the number of test cases. Each test case consists of two lines:
- The first line of each test case contains an integer
n
, the number of elements in the array. - The second line contains
n
space-separated integers representing the array elements.
outputFormat
For each test case, output a single line containing the modified array where all zeros have been moved to the end while the order of non-zero elements remains the same. The numbers should be separated by a single space.
## sample1
4
0 0 0 0
0 0 0 0