Loading
Loading
A hardware tester drives a row of LEDs with a binary counter. Every lit LED draws power, so she wants to know how many LEDs are lit at each count.
Given n, return a list of length n + 1 where entry i is the number of 1 bits in the binary form of i, for every i from 0 to n.
n = 50 = 00 bits1 = 11 bit2 = 10same as 1, shifted: 1 bit3 = 11bits(1) + 1 = 24 = 100bits(2) + 0 = 15 = 101bits(2) + 1 = 2[0, 1, 1, 2, 1, 2]Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
0, 1, 10, 11, 100, 101 in binary.
Example 2
0, 1 and 10 in binary.
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(n) time · O(1) space
Input. Run your code to watch it work on this input, one line at a time.
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.