FunctionRacerProblemsRace

Numbers With K One Bits

HardBitsPython, JavaScript, TypeScript, Java, C++

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

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Bits problems