Skip to content

Practice Gray Code

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#89
LeetCode Problem

Gray Code

Problem Statement

Return any cyclic n-bit Gray code: a sequence containing all 2^n integers from 0 through 2^n - 1 exactly once, with 0 first. Adjacent values must differ in exactly one binary bit, and the final value must also differ from the first value in exactly one bit.

Source: Gray Code on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
n = 2
Output
[0,1,3,2]

# Example 2

Input
n = 1
Output
[0,1]

Constraints

1 <= n <= 16

Related LeetCode Problems

Practice more problems similar to Gray Code.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.