Blind 75
72 / 75Free practice

Counting Bits

Easy · Bit manipulation

Jump to code

For every integer from 0 through n inclusive, return its number of set bits. Assume n ≥ 0.

Examples

Example 1

n = 2

Output: [0,1,1]

Example 2

n = 5

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

Constraints

  • 0 <= n <= 1000
Need a hint?

Removing the last binary digit gives an already computed number.

Real bit manipulation interview questions

Questions on the same topic that candidates reported from real company interviews. Some company questions are locked and open with Premium or XP.

Browse all real coding interview questions

Accepted submissions mark this problem complete. Progress stays in this browser.

Run checks 5 examples. Submit checks all 6 cases. A free account runs this problem; no XP needed.
Language
Case 1
Input
[
  2
]
Expected
[
  0,
  1,
  1
]
Case 2
Input
[
  5
]
Expected
[
  0,
  1,
  1,
  2,
  1,
  2
]
Case 3
Input
[
  0
]
Expected
[
  0
]
Case 4
Input
[
  1
]
Expected
[
  0,
  1
]
Case 5
Input
[
  8
]
Expected
[
  0,
  1,
  1,
  2,
  1,
  2,
  2,
  3,
  1
]

Drafts are saved per language in this browser when storage is available.