Problem of the day
Consider the array { 1, 1, 0, 2, 0 }.
For the given array the modified array should be {0,0,1,1,2} .
Arrays { 0, 0, 1, 2, 1 } and { 0, 0, 2, 1, 1 } are not the correctly reorganized array even if they have all the zero values pushed to the left as in both the arrays the relative order of non-zero elements is not maintained.
Can you solve the problem in linear time, and constant space?
The first line of the input contains an integer 'T' representing the number of test cases or queries to be processed.
Then the 'T' test case follows.
The first line of each test case contains an integer 'N' denoting the number of elements in the array 'ARR'.
The second line of each test contains 'N' space-separated integers denoting the array elements.
For each test case, print the modified array in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
-10^9 <= ARR[i] <= 10^9
Where 'T' denotes the number of test cases, 'N' denotes the number of elements in the array ‘ARR’ respectively, and 'ARR[i]' denotes the ’i-th’ element of the array 'ARR'.
Time limit: 1 second
2
5
1 2 0 0 1
3
1 0 0
0 0 1 2 1
0 0 1
For the first test case, the given array is { 1, 2, 0, 0, 1 }, if we move all the zeros to the left the modified array becomes { 0, 0, 1, 2, 1} which is our final answer.
For the second test case, the given array is { 1, 0, 0}, if we move all the zeros to the left the modified array becomes { 0, 0, 1} which is our final answer.
1
5
1 2 3 4 5
1 2 3 4 5