Skip to content
BASA DSA

Counting Bits

EasyBit Manipulation

For every integer i from zero through n, return its number of one bits.

Example 1

Input
n = 5
Output
[0,1,1,2,1,2]

Explanation

The binary values 0..5 have counts 0,1,1,2,1,2.

Example 2

Input
n = 0
Output
[0]

Explanation

Include zero itself, so n=0 gives a one-element result.

Example 3

Input
n = 8
Output
[0,1,1,2,1,2,2,3,1]

Explanation

8 is binary 1000 with one set bit; retain all preceding counts too.

Constraints

  • 0 <= n <= 100000

Function contractcountBitsArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…