Non-negative Integers without Consecutive Ones

Given a positive integer n, return the number of integers in the range [0, n] whose binary representations do not contain consecutive ones.

Example 1
Inputn = 5
Output5
Among the integers from 0 through 5, only 3 has binary representation 11 with consecutive ones, so the other 5 integers satisfy the rule.
Example 2
Inputn = 1
Output2
Both 0 and 1 have binary representations without consecutive ones.

Constraints

  • 1 <= n <= 10^9

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate