
Input: ‘N’ = 4, ‘NUMS’ = [2, 5, 3, 6]
Output: [5, 3, 2, 6]
Sorting the odd numbers of the array ‘NUMS’ in non-increasing order, the result is 5, 3
Then, Sorting the even numbers in non-decreasing order, the result is 2, 6.
Since the final array should contain the odd numbers in non-increasing order in the first half and even numbers in non-decreasing order in the other half.
So, the final array is [5, 3, 2, 6].
The first line will contain the integer 'T', denoting the number of test cases.
The first line of each test case contains an integer ‘N’ where ‘N’ denotes the length of the array ‘NUMS’.
The second line of each test case contains ‘N’ integers.
For each test case, you don’t need to return anything just rearrange the given array.
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
Sum of ‘N’ <= 10^5
1 <= NUMS[i] <= 10^9
Time Limit: 1 sec
In the naive approach, We store the odd numbers in an extra array and the even numbers in another array. Sort them separately and then replace the first half of ‘NUMS’ with the odd numbers array and the other half with the even numbers array.
We can partition the array ‘NUMS’ in place in such a way that the first half contains only odd numbers and the second half contains only even numbers. The idea is to use a partitioned index that initially points to the starting of the array. Iterate over the array if any odd number is encountered swap it with the partitioned Index and then increment the partitioned index by 1. After partitioning sort the array from start to partitioned index in non-increasing order and the rest in non-decreasing order.