Leetcode 1356: Sort Integers by The Number of 1 Bits

Pierre-Marie Poitevin
3 min readFeb 22, 2020

In this Leetcode problem, we are asked to sort the integer, but not in the natural ordering, instead integers are sorted by the number of 1s in their binary representation.

Here is the problem statement:

Given an integer array arr. You have to sort the integers in the array in ascending order by the number of 1's in their binary representation and in case of two or more integers have the same number of 1's you have to sort them in ascending order.

Return the sorted array.

--

--