The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case will contain one integer ‘N’ that denotes the size of the ‘ARR’.
The second line of each test case contains ‘N’ space-separated integers ‘ARR[i]’, which denote the numbers present in our ‘ARR’.
For each test case, do not return anything, only do changes in the original array/list ‘ARR’.
Output for every test case will be printed in a separate line.
You don’t need to print anything; It has already been taken care of.
Can you solve this in O(1) auxiliary space?
1 <= T <= 10^2
1 <= N <= 10^3
0 <= A[i] <= 10^9
Time Limit: 1 sec
The basic idea of this approach is to iterate the whole ‘ARR’ from start and see if the element present at the current position satisfies the conditions or not. If the element at the current index is not as per requirement then we will find an element which can take that position from ‘ARR’ after that index and replace it with the current element.
Here is the algorithm:
The basic idea of this approach is that we will iterate in the entire array/list ‘ARR’ and find all the appropriate positions of elements in the new array/list ‘TEMP’. Let’s say if the element present in the current position is even then we will add it to the even index in ‘TEMP' and the same for the odd value element in the ‘ARR’ in the odd index position in the new temporary array/list ‘TEMP’.
Here is the algorithm:
The basic idea of this approach is that we will keep two variables which will keep the track of elements present in the ‘ARR’ at their proper positions. We will find all the pairs of mismatches one by one in sequence and swap the odd value mismatches with even value mismatches.
Here is the algorithm: