Numbers With K One Bits
Return how many whole numbers from 0 to n have exactly k one bits in their binary form.
Example 1
n 10
k 2
returns 5
3, 5, 6, 9 and 10 are 11, 101, 110, 1001 and 1010 in binary.
Example 2
n 7
k 0
returns 1
Only 0 has no one bit at all.
Example 3
n 7
k 4
returns 0
Numbers up to 7 have three bits, so none has four 1s.
Constraints
0 <= n <= 10**150 <= k <= 50
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Bits problems
- Numbers Without Neighboring OnesHard
- Smallest Team With Every SkillHard
- Split Powers to Hit a TotalHard
- Build a MaskBeginner
- Fixed Width BinaryBeginner
- Flip One BitBeginner
- Gray Code SequenceBeginner
- How Many Bits LongBeginner