Skip to the code
cf.
+10 XP

#679 Set Bits for Every ID

easy

A feature-flag dashboard numbers flag sets from 0 to n, where each number is a mask. To draw a histogram, it needs the number of flags on in each mask.

Write bitsUpTo(n) that returns an array counts of length n + 1, where counts[i] is the number of 1 bits in i. Aim for O(n) total time.

Example 1

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

0, 1, 10, 11, 100, 101 have 0, 1, 1, 2, 1, and 2 ones.

Example 2

Input: n = 0
Output: [0]

0 has no 1 bits.

Constraints

  • 0 <= n <= 10^5
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).