Counting Bits
Count the 1 bits (set bits) of every number from 0 to n
DSA, Easy, Bit Manipulation
Which structure or algorithm, and what does it cost.
A shop numbers its boxes from 0 to n. Each box label needs the count of 1 digits in the binary form of the box number. Return a list of length n + 1, where the entry at position i is the count for box number i.
Examples
Constraints
nis a whole number from 0 to 50,000.
Your answer must contain
- The simple method and its cost; a method that meets the target cost and why it is correct; code; time and memory cost; a hand trace of example 1.
Target Aim for O(n) time and O(n) memory, with each answer built from an earlier one.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
Write the answers for 0 to 9 side by side. Compare the answer for a number with the answer for a smaller one.
Hint 2 the approach
The count for a number is tied to the count of a smaller number. Think about which smaller number you get by removing one binary digit.
Hint 3 the key step
Drop the last binary digit of i. You get a smaller number that you have already answered. What does the dropped digit add to its count?
Walkthrough
Full walkthrough brute force to best
Brute force
For each number from 0 to n, count its 1 bits on its own by testing every bit position. Numbers up to 50,000 have at most 16 binary digits, so that is about 16 steps for each number. This is O(n log n) time, plus the output list.
Better method
Use the blocks between powers of two. The numbers 8 to 15 are the numbers 0 to 7 with one more 1 bit in front. So the count for i is 1 plus the count for i minus the largest power of two that fits in i. This is O(n) time, and it needs one extra variable for the current power of two.
Best method
The binary form of i is the binary form of i shifted right by one, plus one last bit. So the count for i is the count for i shifted right by one, plus 1 if i is odd. The smaller number is below i, so its answer is already in the list when we reach i. Each entry costs one lookup and one add, with no counting from scratch.
Hand trace
Run on n = 9. The list starts as [0]. For i = 1 (1): drop the last bit to reach 0, the last bit is 1, so 0 + 1 = 1. For i = 2 (10): reach 1, last bit 0, so 1. For i = 3 (11): reach 1, last bit 1, so 2. For i = 4 (100): reach 2, last bit 0, so 1. For i = 5 (101): reach 2, last bit 1, so 2. For i = 6 (110): reach 3, last bit 0, so 2. For i = 7 (111): reach 3, last bit 1, so 3. For i = 8 (1000): reach 4, last bit 0, so 1. For i = 9 (1001): reach 4, last bit 1, so 2. The result is [0, 1, 1, 2, 1, 2, 2, 3, 1, 2].
Code
def count_bits(n):
bits = [0] * (n + 1)
for i in range(1, n + 1):
bits[i] = bits[i >> 1] + (i & 1)
return bitsTime and memory
O(n) time, because each number needs one lookup and one add. O(n) memory for the output list, with no other storage.
Common mistakes
- Counting every number from scratch. That repeats work and costs O(n log n).
- Building a list of length n instead of n + 1. Position n must exist, and for n = 0 the answer is [0].
- Reading the wrong earlier entry, such as the one for i minus 1. The earlier entry must be the one for i shifted right by one.