> ## 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.

# Gray Code Python Solution with Tests

> Tested Python solution for LeetCode 89 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 89, [Medium](/catalog/medium). Topics: [Math](/catalog/topics/math), [Backtracking](/catalog/topics/backtracking), [Bit Manipulation](/catalog/topics/bit-manipulation). [View on LeetCode](https://leetcode.com/problems/gray-code/description/).

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

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

## Problem

An **n-bit gray code sequence** is a sequence of `2^n` integers where:

* Every integer is in the **inclusive** range `[0, 2^n - 1]`,
* The first integer is `0`,
* An integer appears **no more than once** in the sequence,
* The binary representation of every pair of **adjacent** integers differs by **exactly one bit**, and
* The binary representation of the **first** and **last** integers differs by **exactly one bit**.

Given an integer `n`, return *any valid* **n-bit gray code sequence**.

### Examples

```
Input: n = 2
Output: [0,1,3,2]
Explanation: The binary representation of [0,1,3,2] is [00,01,11,10].
- 00 and 01 differ by one bit
- 01 and 11 differ by one bit
- 11 and 10 differ by one bit
- 10 and 00 differ by one bit
[0,2,3,1] is also a valid gray code sequence.
```

```
Input: n = 1
Output: [0,1]
```

### Constraints

* `1 <= n <= 16`

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(2^n)
    # Space: O(1) extra space, excluding the output list
    def gray_code(self, n: int) -> list[int]:
        result = [0]
        for i in range(n):
            result.extend(value | (1 << i) for value in reversed(result))
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(2^n) | O(1) extra space, excluding the output list |

## Tags


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