Gray Code Sequence
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
0 <= n <= 10
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Bits problems
- How Many Bits LongBeginner
- Keep the Low BitsBeginner
- Lowest Set BitBeginner
- Parity BitBeginner
- Read One BitBeginner
- Read a Binary StringBeginner
- Set Bit PositionsBeginner
- Switch Off the Lowest OneBeginner