Generating N-bit Grey Code starting from K

Given a value N and K, the task is to generate N-bits Gray Code starting from the value K.

Examples:

Input: N = 2, K = 3
Output: 3 2 0 1
Explanation:
3 -> 11
2 -> 10
0 -> 00
1 -> 01
Each value differ by only one bit from the next value in their binary representation.



Input: N = 3, K = 2
Output: 2 3 1 0 4 5 7 6

Approach:

  • Gray code are numbers with hamming distance 1 between two consecutive numbers in it.
  • The XOR with each element of N bit Gray code generates a sequence of Hamming distance of 1.
  • As the first element of N bit Gray code is K, it can be obtained by doing is XOR with 0, i.e. (K ^ 0) = K.
  • So the sequence will start with 0 with every consecutive element differ by only one bit in their binary representation.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program Generating N-bit
// Gray Code starting from K
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to Generating N-bit
// Gray Code starting from K
void genSequence(int n, int val)
{
  
    for (int i = 0; i < (1 << n); i++) {
  
        // Generate gray code of corresponding
        // binary number of integer i.
        int x = i ^ (i >> 1) ^ val;
        cout << x << " ";
    }
}
  
// Driver code
int main()
{
    int n = 3, k = 2;
    genSequence(n, k);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program Generating N-bit 
// Gray Code starting from K 
  
class GFG {
      
    // Function to Generating N-bit 
    // Gray Code starting from K 
    static void genSequence(int n, int val) 
    
      
        for (int i = 0; i < (1 << n); i++) { 
      
            // Generate gray code of corresponding 
            // binary number of integer i. 
            int x = i ^ (i >> 1) ^ val; 
            System.out.print(x + " "); 
        
    
      
    // Driver code 
    public static void main (String[] args)
    
        int n = 3, k = 2
        genSequence(n, k); 
      
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program Generating N-bit 
# Gray Code starting from K 
  
# Function to Generating N-bit 
# Gray Code starting from K 
def genSequence(n, val) :
  
    for i in range(1 << n) :
  
        # Generate gray code of corresponding 
        # binary number of integer i. 
        x = i ^ (i >> 1) ^ val; 
        print(x, end= " "); 
  
# Driver code 
if __name__ == "__main__"
  
    n = 3; k = 2
    genSequence(n, k); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program Generating N-bit 
// Gray Code starting from K 
using System;
  
class GFG 
{
      
    // Function to Generating N-bit 
    // Gray Code starting from K 
    static void genSequence(int n, int val) 
    
      
        for (int i = 0; i < (1 << n); i++)
        
      
            // Generate gray code of corresponding 
            // binary number of integer i. 
            int x = i ^ (i >> 1) ^ val; 
            Console.Write(x + " "); 
        
    
      
    // Driver code 
    public static void Main()
    
        int n = 3, k = 2; 
        genSequence(n, k); 
      
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Output:

2 3 1 0 4 5 7 6



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01

Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.