


For example, if the input array is: [0, 1, -2, 3, 4, 0, 5, -27, 9, 0], then the output array must be: [1, -2, 3, 4, 5, -27, 9, 0, 0, 0].
The first line of input contains a single integer ‘T’ representing the number of test cases.
The first line of each test case contains a single integer ‘N’ representing the size of the array. The second line of each test case contains ‘N’ integers representing the elements of the array.
For each test case, modify the input array and print the output in a new line
You don’t need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 50
1 <= N <= 10^6
-10000 <= A[i] <= 10000
Where ‘T’ is the number of test cases, ‘N’ is the size of the array, A[i] is the value of the element present at the ith index.
Time Limit:1sec
Hint: can you think of separating zero elements and non-zero elements in different containers?
We can create a new array of integers. As soon as we find a non-zero element, we can directly insert it into our new array. After that, we can insert all the left zero’s.
We can easily calculate the number of left zeroes as :
Size of original array = size of new array + number of zero’s (since we only took non-zero elements.
Hint: can you maintain two different pointers for zero and non-zero elements and swap them within the same array?
Approach :
We will use two pointers :
Our job is to only push zero’s towards the end, so all the non-zero numbers before any occurrence of zero’s will stay as it is. Hence our Non-zero pointer will point only when we encounter a non-zero element after zero element pointer.
When both of our pointers are set, we can swap the elements present at these places within the same array. Move the zero-pointers ahead. We will also move the non-zero pointer until we find a non zero element.
As soon as the non-zero pointer reaches the end, we will stop.