Parity-Binary Sort
65% Success2256 Attempts20 Points2s Time Limit256MB Memory1024 KB Max Code

You are given an array \(A \) of \(N\) integers. Your task is to implement a sorting algorithm that arranges the numbers in the array based on following conditions and return that array:
1. Numbers with an even count of set bits should appear first in ascending order.
2. Following that, numbers with an odd count of set bits should be arranged in ascending order.

Input Format:

Note: This is the input format that you must use to provide custom input (available above the Compile and Test button).

The first line contains \(T\) denoting the number of test cases. \(T\) also specifies the number of times you have to run the solve function on a different set of inputs.
For each test case:

  • The first line contains the integer \(N\).
  • The second line contains the array \(A\) of length \(N\)

Output Format:
For each test case, print the answer in a new line.

Constraints:

\(1 \leq T \leq 10\)
\(1 \leq N \leq 10^5\)
\(1 \leq A[i] \leq 10^5\)

Examples
Input
1
6
5 2 8 12 7 6
Output
5 6 12 2 7 8
Explanation

Given the input array [5, 2, 8, 12, 7, 6], the output should be [5, 6, 12, 2, 7, 8], as:
Numbers with an even count of set bits: 5(101), 6(110), 12(1100)
Numbers with an odd count of set bits: 2(10), 7(111), 8(100)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading Editor...
Results