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.
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.
Input: n = 0 Output: [0]
0 has no 1 bits.
0 <= n <= 10^5Run 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).
I'm here to help with #679 Set Bits for Every ID. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.