Ninja recently studied odd and even numbers but he is more interested in even numbers.
He has an array ‘A’ containing ‘N’ integers. He will transform this array by moving all the even numbers at the beginning of the array and all the odd numbers at the end of the array.
Output the final array after transformation. If there are multiple transformations possible for the array, output any.
Example :N = 4
A = [ 1, 4, 3, 2 ]
Explanation :
The even numbers are : 4, 2.
The odd numbers are : 1, 3.
The final array ‘A’ is : [ 4, 2, 1, 3 ].
The first line contains an integer 'T' which denotes the number of test cases to be run. Then the test cases follow.
The first line of each test case contains an integer ‘N’.
The next line contains ‘N’ integers denoting the elements of array ‘A’.
Output format :
For each test case, print the final array after transformation.
Print the output of each test case in a new line.
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^5
Sum of N over all Test cases <= 10^5
Time Limit : 1 sec
2
2
1 1
3
3 2 4
1 1
2 4 3
For test case 1 we have,
There are no even numbers and only odd numbers.
So, the array remains the same.
For test case 2 we have,
The even numbers are : 2, 4.
The odd numbers are : 3.
Array ‘A’ after transformation : [ 2, 4, 3 ].
2
2
7 2
5
4 1 7 5 1
2 7
4 1 5 1 7
Can you implement a comparator that prioritizes even numbers.
Approach :
Algorithm :
O(N*log(N)) , where ‘N’ is the length of the array ‘A’.
We are sorting array of size ‘N’ , so the overall time complexity is O(N*logN).
O(1)
Constant extra space is required. Hence, the overall Space Complexity is O(1).