LeetCode #338 Easy

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.

Constraints
  • 0 <= n <= 10⁵
bit-manipulationdynamic-programming
Open on LeetCode ↗
02

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 because i >> 1 is smaller than i.
  • The last bit: i & 1, which is 1 for odd i and 0 for even i.

Filling the array from 0 upward, each entry costs one lookup and one addition.

How to spot this pattern

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.

03

Approach

Try it first

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?

1

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.

2

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.

3

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def countBits(self, n: int) -> List[int]:
▶3 ans = [0] * (n + 1)
▶4 for i in range(1, n + 1):
▶5 ans[i] = ans[i >> 1] + (i & 1)
▶6 return ans
ans00123450 has no 1 bits → fill 1 to 5
n5
ans[0]00 has no 1 bits
Zero has no 1 bits, so 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.
ans001123451 in binary1last0ans[0]+1last bit=1ans[1]1 has 1 one: 0's 0 + last bit 1
i11
i >> 100
i & 11
ans[i]1
1 is 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.
ans0011123452 in binary101last1ans[1]+0last bit=1ans[2]2 has 1 one: 1's 1, last bit 0
i210
i >> 111
i & 10
ans[i]1
2 is 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.
ans00111223453 in binary111last1ans[1]+1last bit=2ans[3]3 has 2 ones: 1's 1 + last bit 1
i311
i >> 111
i & 11
ans[i]2
3 is 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.
ans001112231454 in binary1002last1ans[2]+0last bit=1ans[4]4 has 1 one: 2's 1, last bit 0
i4100
i >> 1210
i & 10
ans[i]1
4 is 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.
ans0011122314255 in binary1012last1ans[2]+1last bit=2ans[5]5 has 2 ones: 2's 1 + last bit 1
i5101
i >> 1210
i & 11
ans[i]2
5 is 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.
ans001112231425return [0, 1, 1, 2, 1, 2]
ans[0, 1, 1, 2, 1, 2]
Done. Each of the 5 entries took one lookup into a smaller entry plus its last bit: one pass, no popcount.
05

Common pitfalls

Leaving out the brackets around i & 1

✗ Wrong
ans[i] = ans[i >> 1] + i & 1
✓ Right
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

✗ Wrong
ans[i] = ans[i - 1] + 1
✓ Right
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

✗ Wrong
ans = [0] * n
✓ Right
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.

06

Edge cases

n = 0

The array is just [0] and the loop from 1 to 0 never runs, so [0] is returned.

07

Complexity

Time
O(n)
Space
O(1)
One lookup, one shift and one addition per number. Besides the output array of n + 1 entries, which the problem asks for, only the loop variable is used.
08

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.