Non-negative Integers without Consecutive Ones

Hard

Description

Given a positive integer n, return the number of non-negative integers less than or equal to n that do not have consecutive 1s in their binary representation.

Examples

Input:n = 5
Output:5
Explanation:

0,1,2,4,5 are valid.

Input:n = 1
Output:2
Explanation:

Edge case with minimal input.

Input:n = 10
Output:8
Explanation:

Valid numbers are 0,1,2,4,5,8,9,10. Numbers 3(11), 6(110), 7(111) have consecutive 1s in binary and are excluded.

Constraints

  • 1 ≤ n ≤ 10⁹

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!