InferenceAI

Enter at least two characters to search.

Start free

Bit Counts

easyBit Manipulation15 min10 XP

Return a list out of length n+1n + 1 where out[i] is the number of 1 bits in the binary form of i.

Requirements

  • Run in O(n)O(n) using the relation bits(i)=bits(i≫1)+(i & 1)\text{bits}(i) = \text{bits}(i \gg 1) + (i \,\&\, 1).

Examples

EXAMPLE WALKTHROUGH0 to 2

A guided view of the published example, not a trace of your code.

INPUT[2]
TASKbit_counts()
EXPECTED OUTPUT[0,1,1]

Start with the input values shown in this example.

Example 1 of 3

Input: n = 2

Output: [0,1,1]

Explanation: 0 to 2

Input: n = 5

Output: [0,1,1,2,1,2]

Explanation: 0 to 5

Input: n = 0

Output: [0]

Explanation: only zero

Hint 1

Shifting right drops the last bit; add it back with i & 1.

Topics

DSABit ManipulationPython