Counting Bits
Given an integer n, return an array of length n + 1 where the element at index i is the number of 1 bits (the popcount) in the binary representation of i.
Open official problem prompt ↗Build the full table of set-bit counts for 0..n in one linear sweep instead of counting each number from scratch.
To count coins in each of many jars, you notice each jar is just a previous jar with one more coin poured off the top. Instead of recounting a jar, you take the known count of the smaller jar and add back the single coin you removed.
- Input
- n = 2
- Output
- [0, 1, 1]
- Why
- 0 = 0b0 has zero 1s, 1 = 0b1 has one, 2 = 0b10 has one.
0 <= n <= 10^5