Practice Counting Bits
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1's in the binary representation of i.
Source: Counting Bits on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
n = 2
[0,1,1]
# Example 2
n = 5
[0,1,1,2,1,2]
Constraints
How to solve Counting Bits
Original notes for interview preparation, written for this problem. Reviewed .
Key insight
The binary form of i is the binary form of i shifted right by one with the lowest bit appended, so the popcount of i is the popcount of i divided by 2 plus (i mod 2), and every answer reuses one already computed.
Why brute force is not enough
Counting set bits of each number independently by looping over its bits costs O(log n) per number, O(n log n) overall, about 1.7 * 10^6 operations for n up to 10^5. That passes, but the problem is designed around the linear recurrence between answers.
Approach
- Allocate an array of length n + 1 and set the entry for 0 to 0, since zero has no set bits.
- For each i from 1 to n, set the entry to the entry at i shifted right by one plus the lowest bit of i; the invariant is that every index below i already holds its correct count.
- The relation holds because the shift drops exactly one bit, the least significant, whose value is i mod 2.
- An equivalent recurrence uses the entry at i with its lowest set bit cleared, plus one; both finish in a single pass.
- Return the array once i reaches n.
Walkthrough of Example 2
n = 5 → [0,1,1,2,1,2]
- The entry for 0 is 0.
- For 1, half is 0 with count 0 and the lowest bit is 1, so 1. For 2, half is 1 with count 1 and the lowest bit is 0, so 1.
- For 3, half is 1 with count 1 and the lowest bit is 1, so 2. For 4, half is 2 with count 1 and the lowest bit is 0, so 1.
- For 5, half is 2 with count 1 and the lowest bit is 1, so 2. The array is [0,1,1,2,1,2], matching the expected output.
Time and space
Time O(n) · Space O(n). n is the input bound; each of the n + 1 entries is computed with one array read, one shift, and one addition, and the output array is the only storage.
Interview pitfalls
- Allocating n entries instead of n + 1, or returning an empty array when n is 0 rather than [0].
- Starting the loop at 0 and reading index -1 through the look-back pattern.
- Memorizing the i and (i - 1) recurrence without being able to explain why clearing the lowest set bit removes exactly one 1.
- Using a modulo operator on signed values in languages where negative results differ; inputs here are non-negative, but a bitwise and with 1 is clearer.
Follow-up questions interviewers ask
- How would you count set bits of a single very large integer quickly, and what does an 8-bit lookup table buy you?
- How would you extend the idea to compute the highest set bit or trailing zeros for every number up to n?
Further reading on the technique: Hamming weight
Related LeetCode Problems
Practice more problems similar to Counting Bits.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.