Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

Leetcode Gray Code problem solution

YASH PAL, 31 July 2024

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

  1. Every integer is in the inclusive range [0, 2n – 1],
  2. The first integer is 0,
  3. An integer appears no more than once in the sequence,
  4. The binary representation of every pair of adjacent integers differs by exactly one bit, and
  5. 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.

Leetcode Gray Code problem solution

Topics we are covering

Toggle
  • Problem solution in Python.
  • Problem solution in Java.
  • Problem solution in C++.
  • Problem solution in C.

Problem solution in Python.

class Solution:
    def grayCode(self, n: int) -> List[int]:
        ans = []
        for i in range(0, 2 ** n):
            b = bin(i)[2:]
            temp = b[0]
            for j in range(1, len(b)):
                temp += str(int(b[j - 1]) ^ int(b[j]))
            ans.append(temp)
        
        return list(map(lambda x: int(x, 2), ans))

Problem solution in Java.

class Solution {
    public List<Integer> grayCode(int n) {
        ArrayList<Integer> res = new ArrayList<>();
        int num = 1<<n;
        for(int i = 0; i < num; i++) {
            res.add((i>>1)^i);
        }
        return res;
    }
}

Problem solution in C++.

vector<int> grayCode(int n) {
        if(n == 0) return {0};
        vector<int> ret = {0, 1};
        
        for(int i=2; i<=n; i++)
            for(int j=ret.size()-1; j>=0; j--)
                ret.push_back(ret[j]+(1<<i-1));
        
        return ret;
}

Problem solution in C.

int* grayCode(int n, int* returnSize) {
    *returnSize=1<<n;
    unsigned int* pAns=(unsigned int*)malloc(*returnSize*sizeof(unsigned int));
    pAns[0]=0;
    for (unsigned int i=1;i<*returnSize;i++){
        unsigned int temp=i;
        for (int j=0;j<n;j++){//xor the last bit which is not zero
            if (temp%2!=0){
                pAns[i]=pAns[i-1]^(1<<j);
                break;
            }
            temp=temp>>1;
        }
    }
    return pAns;
}

coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes