Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add explanation and solution for LeetCode problem 89 (Gray Code) #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
romankurnovskii wants to merge 1 commit into main
base: main
Choose a base branch
Loading
from pr-problem-89
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions explanations/89/en.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
## 89. Gray Code [Medium]

https://leetcode.com/problems/gray-code

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

- Every integer is in the **inclusive** range `[0, 2n - 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**

```tex
Example 1:
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, whose binary representation is [00,10,11,01].
- 00 and 10 differ by one bit
- 10 and 11 differ by one bit
- 11 and 01 differ by one bit
- 01 and 00 differ by one bit

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

**Constraints**
```tex
- 1 <= n <= 16
```

## Explanation

### Strategy
Let's restate the problem: You need to generate a sequence of `2n` numbers from 0 to `2n - 1` where each adjacent pair differs by exactly one bit, and the first and last numbers also differ by exactly one bit.

This is a **bit manipulation problem** that involves understanding binary representations and finding patterns in how numbers can be arranged to satisfy the Gray code properties.

**What is given?** An integer `n` representing the number of bits.

**What is being asked?** Generate any valid n-bit Gray code sequence.

**Constraints:** `n` is between 1 and 16, so the sequences can be quite long.

**Edge cases:**
- `n = 1`: Simple case with only two numbers
- `n = 2`: Small enough to visualize easily
- Large `n`: Requires efficient algorithm

**High-level approach:**
The solution involves understanding the mathematical properties of Gray codes and using a recursive or iterative approach to generate them.

**Decomposition:**
1. **Understand Gray code properties**: Each adjacent pair differs by exactly one bit
2. **Use reflection method**: Build larger Gray codes from smaller ones
3. **Generate sequence**: Create the complete sequence efficiently
4. **Return result**: Return the valid Gray code sequence

**Brute force vs. optimized strategy:**
- **Brute force**: Try all possible permutations and check Gray code properties. This is extremely inefficient.
- **Optimized**: Use the reflection method or mathematical properties to generate Gray codes directly.

### Steps
Let's walk through the solution step by step using the example `n = 2`:

**Step 1: Start with base case**
- For `n = 1`, the Gray code is `[0, 1]`
- Binary: `[0, 1]` or `[00, 01]`

**Step 2: Use reflection method**
- Take the existing sequence: `[0, 1]`
- Reflect it: `[1, 0]`
- Add prefix `0` to first half: `[00, 01]`
- Add prefix `1` to reflected half: `[11, 10]`
- Combine: `[00, 01, 11, 10]`

**Step 3: Convert to decimal**
- `00` = 0
- `01` = 1
- `11` = 3
- `10` = 2
- Result: `[0, 1, 3, 2]`

**Step 4: Verify properties**
- Adjacent pairs differ by exactly one bit:
- `00` and `01`: differ in last bit
- `01` and `11`: differ in first bit
- `11` and `10`: differ in last bit
- `10` and `00`: differ in first bit
- First and last differ by exactly one bit: `00` and `10` differ in first bit

**Why this works:**
The reflection method works because:
1. **Prefix property**: Adding `0` or `1` as a prefix preserves the one-bit difference property
2. **Reflection symmetry**: Reflecting the sequence and adding `1` as prefix creates the necessary transitions
3. **Mathematical induction**: If it works for `n-1`, it works for `n`

> **Note:** The key insight is that Gray codes can be built recursively by reflecting smaller Gray codes and adding appropriate prefixes. This is much more efficient than trying to find valid sequences through brute force.

**Time Complexity:** O(2n) - we generate exactly 2n numbers
**Space Complexity:** O(2n) - we need to store the entire sequence
28 changes: 28 additions & 0 deletions solutions/89/01.py
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
def grayCode(n):
"""
Generate an n-bit Gray code sequence.

Args:
n: int - Number of bits for the Gray code sequence

Returns:
List[int] - Valid n-bit Gray code sequence
"""
# Handle edge case
if n == 0:
return [0]

# Start with base case: 1-bit Gray code
result = [0, 1]

# Build larger Gray codes using reflection method
for i in range(2, n + 1):
# Get the size of the current sequence
size = len(result)

# Reflect the current sequence and add prefix '1'
for j in range(size - 1, -1, -1):
# Add 2^(i-1) to create the reflected part with prefix '1'
result.append(result[j] + (1 << (i - 1)))
Comment on lines +24 to +26
Copy link
Contributor

@sourcery-ai sourcery-ai bot Aug 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (code-quality): Replace a for append loop with list extend (for-append-to-extend)

sourcery-ai[bot] reacted with thumbs up emoji sourcery-ai[bot] reacted with thumbs down emoji

return result

AltStyle によって変換されたページ (->オリジナル) /