FunctionRacerProblemsRace

Gray Code Sequence

BeginnerBitsPython, JavaScript, TypeScript, Java, C++

A Gray code sequence of n bits lists all 2n numbers from 0 to 2n - 1 so that neighbouring numbers differ in exactly one bit. There are many such sequences, so build this one: the value at position i is i ^ (i >> 1). Return the list for n bits.

Example 1

n 2
returns [0,1,3,2]

Positions 0 to 3 give 0, 1, 3, 2.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Bits problems