Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given an integer array arr. 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 array after sorting it.
Example 1:
Input: arr = [0,1,2,3,4,5,6,7,8] Output: [0,1,2,4,8,3,5,6,7] Explantion: [0] is the only integer with 0 bits. [1,2,4,8] all have 1 bit. [3,5,6] have 2 bits. [7] has 3 bits. The sorted array by bits is [0,1,2,4,8,3,5,6,7]
Example 2:
Input: arr = [1024,512,256,128,64,32,16,8,4,2,1] Output: [1,2,4,8,16,32,64,128,256,512,1024] Explantion: All integers have 1 bit in the binary representation, you should just sort them in ascending order.
Constraints:
1 <= arr.length <= 5000 <= arr[i] <= 104Problem summary: You are given an integer array arr. 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 array after sorting it.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Bit Manipulation
[0,1,2,3,4,5,6,7,8]
[1024,512,256,128,64,32,16,8,4,2,1]
find-subsequence-of-length-k-with-the-largest-sum)find-if-array-can-be-sorted)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1356: Sort Integers by The Number of 1 Bits
class Solution {
public int[] sortByBits(int[] arr) {
int n = arr.length;
for (int i = 0; i < n; ++i) {
arr[i] += Integer.bitCount(arr[i]) * 100000;
}
Arrays.sort(arr);
for (int i = 0; i < n; ++i) {
arr[i] %= 100000;
}
return arr;
}
}
// Accepted solution for LeetCode #1356: Sort Integers by The Number of 1 Bits
func sortByBits(arr []int) []int {
for i, v := range arr {
arr[i] += bits.OnesCount(uint(v)) * 100000
}
sort.Ints(arr)
for i := range arr {
arr[i] %= 100000
}
return arr
}
# Accepted solution for LeetCode #1356: Sort Integers by The Number of 1 Bits
class Solution:
def sortByBits(self, arr: List[int]) -> List[int]:
return sorted(arr, key=lambda x: (x.bit_count(), x))
// Accepted solution for LeetCode #1356: Sort Integers by The Number of 1 Bits
impl Solution {
pub fn sort_by_bits(mut arr: Vec<i32>) -> Vec<i32> {
let n = arr.len();
for i in 0..n {
arr[i] += arr[i].count_ones() as i32 * 100000;
}
arr.sort();
for i in 0..n {
arr[i] %= 100000;
}
arr
}
}
// Accepted solution for LeetCode #1356: Sort Integers by The Number of 1 Bits
function sortByBits(arr: number[]): number[] {
const n = arr.length;
for (let i = 0; i < n; ++i) {
arr[i] += bitCount(arr[i]) * 100000;
}
arr.sort((a, b) => a - b);
for (let i = 0; i < n; ++i) {
arr[i] %= 100000;
}
return arr;
}
function bitCount(i: number): number {
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.