Problems > Order by Set Bits

Order by Set Bits

Difficulty: 1100 3500 ms 128 MB

sorting

Sort the values by the number of 1-bits in their binary form; break ties by the value itself (ascending).

Input: the first line has N; the second line has N integers.

Output: the values in the required order.

Constraints:

  • 1 ≤ N ≤ 105
  • 0 ≤ ai ≤ 109

Sample Tests

Input 1
5
12 5 30 7 19
Output 1
5 12 7 19 30
Input 2
2
100 2
Output 2
2 100

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.