Counting Bits
Counting Bits is LeetCode 338 (Easy). Given an integer n, return an array ans of length n + 1 where ans[i] is the number of 1s in the binary form of i, for every i from 0 to n.
n goes up to 10⁵. Counting each number's bits separately takes O(n log n); the follow-up asks for O(n) in a single pass, without a built-in popcount.
- 0 <= n <= 10⁵
Intuition
Every number's bit count can be read off a smaller number whose count is already in the array.
Shifting i right by one, i >> 1, drops its last binary digit and keeps all the others. So i has exactly the 1s of i >> 1, plus one more if the dropped digit was a 1:
- The other bits:
ans[i >> 1], already filled in becausei >> 1is smaller thani. - The last bit:
i & 1, which is 1 for oddiand 0 for eveni.
Filling the array from 0 upward, each entry costs one lookup and one addition.
When the answer is wanted for every number from 0 to n, look for a smaller number whose answer explains this one. For bit counts that smaller number is i >> 1 (or i & (i - 1)), the same reuse that makes this a one-pass table.
Approach
Before reading on: write 0 to 8 in binary next to their counts of 1s. How does the count for 6 (110) relate to the count for 3 (11)? And 7 (111) to 3?
Start with ans[0] = 0
Make an array of n + 1 zeros. Zero has no 1 bits, so ans[0] = 0 is already correct, and every other entry will be built from a smaller one.
Split i into its last bit and the rest
Check the split on i = 13 (1101): 13 >> 1 = 6 (110, two 1s) and 13 & 1 = 1, so 13 has 2 + 1 = 3 ones.
Fill the array from left to right
For i from 1 to n, set ans[i] = ans[i >> 1] + (i & 1). i >> 1 is smaller than i, so its entry is filled in before i needs it. When the loop ends, return ans.
Code in Python, C++ and Java
ans[0] = 0 is known from the start. Every later entry is built from a smaller one, so filling from left to right always finds what it needs.1 in binary. Dropping its last digit leaves nothing, which is 0 with 0 ones. The dropped digit is 1 (1 is odd), so ans[1] = 0 + 1 = 1.10 in binary. Dropping its last digit leaves 1 = 1, and ans[1] = 1 is already filled in. The dropped digit is 0 (2 is even), so ans[2] = 1 + 0 = 1.11 in binary. Dropping its last digit leaves 1 = 1, and ans[1] = 1 is already filled in. The dropped digit is 1 (3 is odd), so ans[3] = 1 + 1 = 2.100 in binary. Dropping its last digit leaves 10 = 2, and ans[2] = 1 is already filled in. The dropped digit is 0 (4 is even), so ans[4] = 1 + 0 = 1.101 in binary. Dropping its last digit leaves 10 = 2, and ans[2] = 1 is already filled in. The dropped digit is 1 (5 is odd), so ans[5] = 1 + 1 = 2.ans[0] = 0 is known from the start. Every later entry is built from a smaller one, so filling from left to right always finds what it needs.1 in binary. Dropping its last digit leaves nothing, which is 0 with 0 ones. The dropped digit is 1 (1 is odd), so ans[1] = 0 + 1 = 1.10 in binary. Dropping its last digit leaves 1 = 1, and ans[1] = 1 is already filled in. The dropped digit is 0 (2 is even), so ans[2] = 1 + 0 = 1.ans[0] = 0 is known from the start. Every later entry is built from a smaller one, so filling from left to right always finds what it needs.1 in binary. Dropping its last digit leaves nothing, which is 0 with 0 ones. The dropped digit is 1 (1 is odd), so ans[1] = 0 + 1 = 1.10 in binary. Dropping its last digit leaves 1 = 1, and ans[1] = 1 is already filled in. The dropped digit is 0 (2 is even), so ans[2] = 1 + 0 = 1.11 in binary. Dropping its last digit leaves 1 = 1, and ans[1] = 1 is already filled in. The dropped digit is 1 (3 is odd), so ans[3] = 1 + 1 = 2.100 in binary. Dropping its last digit leaves 10 = 2, and ans[2] = 1 is already filled in. The dropped digit is 0 (4 is even), so ans[4] = 1 + 0 = 1.101 in binary. Dropping its last digit leaves 10 = 2, and ans[2] = 1 is already filled in. The dropped digit is 1 (5 is odd), so ans[5] = 1 + 1 = 2.110 in binary. Dropping its last digit leaves 11 = 3, and ans[3] = 2 is already filled in. The dropped digit is 0 (6 is even), so ans[6] = 2 + 0 = 2.111 in binary. Dropping its last digit leaves 11 = 3, and ans[3] = 2 is already filled in. The dropped digit is 1 (7 is odd), so ans[7] = 2 + 1 = 3.1000 in binary. Dropping its last digit leaves 100 = 4, and ans[4] = 1 is already filled in. The dropped digit is 0 (8 is even), so ans[8] = 1 + 0 = 1.Common pitfalls
Leaving out the brackets around i & 1
ans[i] = ans[i >> 1] + i & 1
ans[i] = ans[i >> 1] + (i & 1)
In Python, C++ and Java, + binds tighter than &, so the line computes (ans[i >> 1] + i) & 1, which is only ever 0 or 1. For i = 3 it gives 0 instead of 2.
Building from i − 1
ans[i] = ans[i - 1] + 1
ans[i] = ans[i >> 1] + (i & 1)
Adding 1 can carry through several bits: 7 is 111 with three 1s, but 8 is 1000 with one. Halving always removes exactly one digit, so it gives a clean rule.
Making the array n long
ans = [0] * n
ans = [0] * (n + 1)
The answer covers 0 through n, which is n + 1 numbers. With n slots the last write, ans[n], is out of range.
Edge cases
The array is just [0] and the loop from 1 to 0 never runs, so [0] is returned.
Complexity
Counting Bits FAQ
Is there another rule for filling the table?
Yes. i & (i - 1) clears the lowest 1 bit of i, so ans[i] = ans[i & (i - 1)] + 1. It is just as fast. A third version adds 1 to the count of i minus the largest power of two not above it, but it has to track that power as the loop runs.
Can I just use bin(i).count('1') or a built-in popcount?
It gives the right answer, and for n up to 10⁵ it passes. But it counts each number from scratch, O(log n) work per number, and the follow-up explicitly asks for one O(n) pass without built-in functions like __builtin_popcount or Integer.bitCount.