Numbers Without Neighboring Ones
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
0 <= n < 2**31
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Bits problems
- 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
- Keep the Low BitsBeginner