Given a non-negative integer n, return an array answer where answer[i] is the number of 1 bits in the binary representation of i for every 0 <= i <= n.
Input / output
n: intint[] of length n + 1Examples
n = 2 returns [0,1,1].
0 has zero set bits, 1 has one, and 2 (10 in binary) also has one.n = 5 returns [0,1,1,2,1,2].
3 is 11 and 5 is 101, so both contribute two set bits.n = 0 returns [0].
The range still includes zero itself.Constraints
0 <= n <= 10^5Edge cases
n can be zero.0 through n in order.Target complexity
O(n) time and O(n) output space.Hints
i >> 1 drops the least significant bit of i.i equals the bit count of i >> 1 plus the final bit i & 1.Follow-up
Can you derive the same recurrence using i & (i - 1) instead?