You are given a 0-indexed array ‘NUMS’ consisting of ‘N’ integers. You need to rearrange the array ‘NUMS’ so that numbers at even positions are greater than or equal to those at odd positions.
E.g. ‘NUMS[ i ]’ >= ‘NUMS[ i - 1 ]’ if ‘i’ is even and ‘NUMS[ i ]’ <= ‘NUMS[ i - 1 ]’ if ‘i’ is odd.
If there are multiple ways to rearrange the array, you can choose any.
Example:Input: ‘N’ = 4, ‘NUMS’ = [2, 3, 6, 5]
Output: [3, 2, 6, 5]
At ‘i’ = 0, ‘NUMS[ i ]’ = 3
At ‘i’ = 1, ‘NUMS[ i ]’ = 2, it satisfies the condition ‘NUMS[ i ]’ <= ‘NUMS[ i - 1 ]’ if ‘i’ is odd.
At ‘i’ = 2, ‘NUMS[ i ]’ = 6, it satisfies the condition ‘NUMS[ i ]’ >= ‘NUMS[ i - 1 ]’ if ‘i’ is even.
At ‘i’ = 3, ‘NUMS[ i ]’ = 5, it satisfies the condition ‘NUMS[ i ]’ <= ‘NUMS[ i - 1 ]’ if ‘i’ is odd.
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’ denoting the length of the array ‘NUMS’.
The second line of each test case contains ‘N’ integers.
Output format :
For each test case, you don’t need to return anything. Just rearrange the given input array.
You may use print statements to debug your code for custom input as the verdict/output will be shown as '1' for correct answer and '0' for incorrect answer.
Note :
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
1 <= A[i] <= 10^9
Sum of ‘N’ <= 10^5
Time Limit: 1 sec
2
4
4 9 2 6
2
1 2
9 4 6 2
2 1
For the first case:
At ‘i’ = 0, ‘NUMS[ i ]’ = 9
At ‘i’ = 1, ‘NUMS[ i ]’ = 4, it satisfies the condition ‘NUMS[ i ]’ <= ‘NUMS[ i - 1 ]’ if ‘i’ is odd.
At ‘i’ = 2, ‘NUMS[ i ]’ = 6, it satisfies the condition ‘NUMS[ i ]’ >= ‘NUMS[ i - 1 ]’ if ‘i’ is even.
At ‘i’ = 3, ‘NUMS[ i ]’ = 2, it satisfies the condition ‘NUMS[ i ]’ <= ‘NUMS[ i - 1 ]’ if ‘i’ is odd.
For the second case:
At ‘i’ = 0, ‘NUMS[ i ]’ = 2
At ‘i’ = 1, ‘NUMS[ i ]’ = 1, it satisfies the condition ‘NUMS[ i ]’ <= ‘NUMS[ i - 1 ]’ if ‘i’ is odd.
2
6
20 12 1 28 16 20
5
2 14 29 21 11
20 1 28 12 20 16
14 2 29 11 21
Can you think of a way to assign only smaller elements at the odd indices?
In the naive approach, we can assign the smallest ceil( N/2 ) numbers at odd positions and the other N/2 numbers at the even positions. In this way, numbers at even positions will always be larger than the numbers at odd positions. To implement this approach, we can iterate over the even indices from left to right and odd indices from right to left and keep on swapping the values of odd positions and even positions.
The steps are as follows:-
Function rearrangeArray(vector<int>& nums):
O( N * log( N ) ), Where ‘N’ denotes the length of the array ‘NUMS’.
We are sorting the ‘NUMS’ array, which takes the time complexity O( N * log( N ) ). Then we are iterating over the original array ‘NUMS’ to modify it. It has the time complexity of O( N ).
Hence the time complexity is O( N * log( N ) ).
O( 1 ).
We are not using any extra storage.
Hence space complexity is O( 1 ).