Tap here to switch tabs
Problem
Submissions

Problem: Gray Code

Gray Code asks you to return a sequence containing every n-bit integer exactly once, starting with zero, where neighboring values, including the endpoints, differ by one bit.

Statement

An nn bit Gray code sequence is a sequence of 2n2^n integers where:

  • The first integer is 00.

  • Every integer appears at most once in the sequence.

  • The binary representation of every pair of adjacent integers differs by exactly one bit.

  • The binary representation of the first and last integers differs by exactly one bit.

Given an integer n, return any valid nn bit Gray code sequence.

Constraints:

  • 1≤1 \leq n ≤16\leq 16

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Gray Code

Gray Code asks you to return a sequence containing every n-bit integer exactly once, starting with zero, where neighboring values, including the endpoints, differ by one bit.

Statement

An nn bit Gray code sequence is a sequence of 2n2^n integers where:

  • The first integer is 00.

  • Every integer appears at most once in the sequence.

  • The binary representation of every pair of adjacent integers differs by exactly one bit.

  • The binary representation of the first and last integers differs by exactly one bit.

Given an integer n, return any valid nn bit Gray code sequence.

Constraints:

  • 1≤1 \leq n ≤16\leq 16