Leetcode Gray Code problem solution

In the Leetcode Gray Code problem solution An n-bit gray code sequence is a sequence of 2n integers where:

Every integer is in the inclusive range [0, 2n – 1],
The first integer is 0,
An integer appears no more than once in the sequence,
The binary representation of every pair of adjacent integers differs by exactly one bit, and
The binary representation of the first and last integers differs by exactly one bit.
Given an integer n, return any valid n-bit gray code sequence.

Example 1:

Input: n = 2
Output: [0,1,3,2]
Explanation:
The binary representation of [0,1,3,2] is [00,01,11,10].

  • 00 and 01 differ by one bit
  • 01 and 11 differ by one bit
  • 11 and 10 differ by one bit
  • 10 and 00 differ by one bit
    [0,2,3,1] is also a valid gray code sequence, whose binary representation is [00,10,11,01].
  • 00 and 10 differ by one bit
  • 10 and 11 differ by one bit
  • 11 and 01 differ by one bit
  • 01 and 00 differ by one bit

Example 2:

Input: n = 1
Output: [0,1]

Constraints:

  • 1 <= n <= 16

Solution in C Programming

int *grayCode(int n, int *outputSize) {
*outputSize = 1 << n;
int *res = (int *)malloc(sizeof(int) * (*outputSize));

res[0] = 0; 
for(int i = 1; i < *outputSize; i++){
    res[i] = res[i - 1] ^ (i & (~i + 1));
}
return res;
}

Solution in C++ Programming

class Solution {
public:
    vector<string> helper(int n){
          if(n==1){
        vector<string> res;
        res.push_back("0");
        res.push_back("1");
        return res;
    }
 vector<string> prev = helper(n-1);
 vector<string> res; 
    for(int i=0;i<prev.size();i++){
        res.push_back("0"+prev[i]);
    }   
        for(int i=prev.size()-1;i>=0;i--){
        res.push_back("1"+prev[i]);
    }  
        return res;
    }
    
    vector<int> grayCode(int n) {
    vector<string> res = helper(n);
    vector<int> mres;
    for(int i=0;i<res.size();i++){
       mres.push_back(stoi(res[i],0,2));
    }
        return mres;
    }
};

Solution in Java Programming

class Solution {
    public List<Integer> grayCode(int n) {

        List<Integer> ans = new LinkedList<Integer>();

        if (n == 0) {
            ans.add(0);
            return ans;
        }
        
        List<Integer> previous = grayCode(n - 1);

        int factor = 1 << (n - 1);

        for (int num : previous) {
            ans.add(ans.size() / 2, num);
            ans.add(1 + ans.size() / 2, num + factor);
        }
        return ans;
    }
}

Solution in Python Programming

class Solution(object):
    def grayCode(self, n):
        if n == 0: return [0]
        ans = [0, 1]
        for i in xrange(2,n+1):
            for j in xrange(len(ans)-1, -1, -1):
                ans.append(ans[j] | 1<<i-1)
        return ans

Solution in C# Programming

public class Solution {
    public IList<int> GrayCode(int n) {
        IList<int> Result = new List<int>();
        Result.Add(0);
        if (n == 0) return Result;
        Stack<int> stack = new Stack<int>();
        stack.Push(0);
        int Curr = 1;
        for (int i = 0; i < n; i++)
        {
            int m;
            while (stack.Count > 0)
            {
            m = stack.Pop();
            Result.Add(m + Curr);                
            }
            Curr = Curr * 2;
            foreach (int j in Result)
            {
                stack.Push(j);
            }
        }
        return Result;
    }
}

By Neha Singhal

Hi, my name is Neha singhal a software engineer and coder by profession. I like to solve coding problems that give me the power to write posts for this site.

Leave a Reply

Your email address will not be published. Required fields are marked *