Parity-Binary Sort
Practice
3.8 (14 votes)
Basics of bit manipulation
Bitmask
Implementation
Merge sort
Bit manipulation
Algorithms
Basic programming
Sorting
Basics of bit manipulation
Problem
65% Success 2180 Attempts 20 Points 2s Time Limit 256MB Memory 1024 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\)

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...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
110 votes
Tags:
Basic ProgrammingBit manipulationEasy
Points:20
156 votes
Tags:
Ad-HocApprovedBit manipulationEasyOpen
Points:30
150 votes
Tags:
ReadyBrute-force searchEasy-MediumApprovedBreadth-first search