Input: ‘N’ = 6
‘A’ = [-1, 2, -3, 1, 13, -10]
Output: [2, 1, 13, -1, -3, 10]
Explanation: In the output array, all the negative elements come after positive elements, and we can also see that the order of positive elements and negative elements is the same, i.e., 2 comes before 1 and 13 in the final array because in the array ‘A’, 2 comes before 1 and 13, and for all other elements, this condition follows.
First-line contains 'T', denoting the number of Test cases.
For each Test case:
The first line contains two integers, ‘N’, denoting the size of the array ‘A’.
The following line contains ‘N’ integers, denoting the array ‘A’.
Return an array in which all the negative numbers are at the end of the array, but the relative order of positive and negative elements is the same.
You don't need to print anything. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^3
-1e9 <= A[i] <= 10^9, A[i] != 0
Time Limit: 1-sec
Consider two arrays, posArray and negArray, containing positive and negative elements, respectively.
Start from the left of the array ‘A’ and go to the right end of the array ‘A’, and at each index, check whether the element is positive or negative and insert the element in the respective array. This way, after the whole iteration, posArray will have all the positive elements, and negArray will have all the negative elements. Still, in both arrays, the relative order will be the same.
The steps are as follows:-
// Function to move all the negative elements to the end of the array
function negativeToTheEnd(int[] a, int n):