Gray Code Problem

Gray Code Problem — ExecCode Medium DSA Practice

Solve the Gray Code problem on ExecCode. Free online medium DSA practice in Bit Manipulation. Write and run code in Java, C++, Python — no signup required to run.

Problem description

An n-bit gray code sequence is a sequence of 2^n integers where every pair of adjacent values differs by exactly one bit, and the first and last values also differ by one bit. Given n, return any valid n-bit gray code sequence starting at 0.

Examples

Input n = 2; Output [0, 1, 3, 2]. Input n = 1; Output [0, 1]. Input n = 3; Output [0, 1, 3, 2, 6, 7, 5, 4]

Constraints

1 <= n <= 16

Practice Gray Code free on ExecCode. Browse DSA problems, topic map, and placement guides.