bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 89. Gray Code

89. Gray Code

Leetcode Solutions

Approach: Iteration with a single loop

  1. Initialize sequenceLength variable to 2^n. sequenceLength denotes the total number of elements present in the Gray code sequence.
  2. Run a for loop for i = 0 to sequenceLength - 1.
    • At each iteration calculate the next number to be added in the sequence using the formula G(i) = i XOR (i >> 1) and assign it to num.
    • Add num to the result.
  3. Return result list.
UML Thumbnail

Approach: Recursion

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...