Given an integer n, return an array ans of length n + 1 where ans[i] is the number of 1 bits in the binary form of i, for every i from 0 to n.
Examples
Example 1
Input: n = 2
Output: [0,1,1]
In binary, 0 is 0, 1 is 1 and 2 is 10. They have zero, one and one 1 bits.
Example 2
Input: n = 5
Output: [0,1,1,2,1,2]
3 is 11 (two 1 bits), 4 is 100 (one) and 5 is 101 (two).
Example 3
Input: n = 0
Output: [0]
The array still has n + 1 = 1 entry: the count for 0.
Example 4
Input: n = 8
Output: [0,1,1,2,1,2,2,3,1]
7 is 111, so it has three 1 bits. 8 is 1000 and has just one, like every power of two.
Constraints
0 <= n <= 10^5
Follow-up
Counting the bits of each number separately takes O(n log n) time. Can you do it in O(n), using the answers you've already worked out?