> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Non-negative Integers without Consecutive Ones

> Tested Python solution for LeetCode 600 with 30 pytest cases. Generate a practice environment with lcpy.

LeetCode 600, [Hard](/catalog/hard). Topics: [Dynamic Programming](/catalog/topics/dynamic-programming). [View on LeetCode](https://leetcode.com/problems/non-negative-integers-without-consecutive-ones/description/).

Generate this problem as a practice environment: tested reference solution, 30 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 600   # by problem number
lcpy gen -s non_negative_integers_without_consecutive_ones   # by problem name
```

## Problem

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

### Examples

```
Input: n = 5
Output: 5
Explanation:
Here are the non-negative integers <= 5 with their corresponding binary representations:
0 : 0
1 : 1
2 : 10
3 : 11
4 : 100
5 : 101
Among them, only integer 3 disobeys the rule (two consecutive ones) and the other 5 satisfy the rule.
```

```
Input: n = 1
Output: 2
```

```
Input: n = 2
Output: 3
```

### Constraints

* 1 \<= n \<= 10^9

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/non_negative_integers_without_consecutive_ones/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/non_negative_integers_without_consecutive_ones/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(log n)
    # Space: O(log n)
    def find_integers(self, n: int) -> int:
        bits = bin(n)[2:]
        fib = [1, 2]
        while len(fib) < len(bits):
            fib.append(fib[-1] + fib[-2])

        count = 0
        prev_bit = 0
        for i, bit_char in enumerate(bits):
            if bit_char == "1":
                count += fib[len(bits) - i - 1]
                if prev_bit == 1:
                    count -= 1
                    break
                prev_bit = 1
            else:
                prev_bit = 0
        return count + 1
```

## Complexity

| Time | Space |
| - | - |
| O(log n) | O(log n) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.