Parity-Binary Sort

3.8

14 votes
Basics of bit manipulation, Bitmask, Implementation, Merge Sort, Bit Manipulation, Algorithms, Basic Programming, Sorting, Basics of Bit Manipulation
Problem

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:

1T101N1051A[i]105

Time Limit: 2
Memory Limit: 256
Source Limit:
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)

Editor Image

?