Ctrl K

No Adjacent Ones

Hard
You get a positive integer limit. Count the integers from 0 to limit inclusive whose binary representation never has two ones next to each other.
Example 1
limit=6 → 5

In binary, 0…6 are 0, 1, 10, 11, 100, 101, 110. 3 (11) and 6 (110) have two ones in a row, which leaves 5 numbers.

Example 2
limit=8 → 6

The valid ones are 0, 1, 2, 4, 5 and 8, six in total.

Example 3
limit=1 → 2
Constraints
  • 1 ≤ limit ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPattern With Dot and Star HardLongest Balanced Bracket Stretch HardWater Between Walls HardDoes the Text Fit the Pattern HardBiggest Solid Panel Block Hard
loading Python…
Input
limit=6
Expected
5

Run checks the first 3 tests in your browser. Submit checks all 27 tests on the server, hidden ones included.