


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
The idea is to use an extra vector to store all the non-zero elements while maintaining their relative order. So we will first add all the non-zero elements to a vector and then iterate that vector backwards and start updating the array values from end. In the end we will set all the array values whose values were not updated to 0.
The idea is to use two pointers reader and writer to store the current reading index and the current writing index. We will initially place both the pointers at the end of the array. Then we will iterate the array backwards using reader pointer and for every non-zero element update the value of the element stored at writer pointer and decrement writer pointer by 1. In the end we will set all the array values whose values were not updated to 0.