FunctionRacerProblemsRace

Numbers Without Neighboring Ones

HardBitsPython, JavaScript, TypeScript, Java, C++

Return how many whole numbers from 0 to n have no two 1 bits next to each other in their binary form.

Example 1

n 8
returns 6

0, 1, 2, 4, 5 and 8 count. In binary 3, 6 and 7 hold two 1s side by side.

Example 2

n 1
returns 2

Only 0 and 1 are in range, and both count.

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