Generate n-bit Gray Codes | Set 2
Last Updated :
24 May, 2021
Given a number n, generate bit patterns from 0 to 2^n-1 such that successive patterns differ by one bit.
Examples:
Input: n=2
Output: 00 01 11 10
Explanation:
Every adjacent element of gray code differs only by one bit. So the n bit grey codes are: 00 01 11 10
Input: n=3
Output: 000 001 011 010 110 111 101 100
Explanation:
Every adjacent element of gray code differs only by one bit. So the n bit gray codes are: 000 001 011 010 110 111 101 100
Another approach of Generate n-bit Gray Codes has already been discussed.
Approach:
The idea is to get gray code of binary number using XOR and Right shift operation.
- The first bit(MSB) of the gray code is same as the first bit(MSB) of binary numbers.
- The second bit(from left side) of the gray code equals to XOR of first bit(MSB) and second bit(2nd MSB) of the binary number.
- The third bit(from left side) of the gray code equals to XOR of the second bit(2nd MSB) and a third bit(3rd MSB) and so on..
In this way, the gray code can be calculated for the corresponding binary number. So, it can be observed that the ith element can be formed by bitwise XOR of i and floor(i/2) which is equal to the bitwise XOR of i and (i >> 1) i.e., i right-shifted by 1. By performing this the MSB of the binary number is kept intact and all the other bits are performed bitwise XOR with its adjacent higher bit.
C++
#include <bits/stdc++.h>
using namespace std;
void decimalToBinaryNumber( int x, int n)
{
int * binaryNumber = new int (x);
int i = 0;
while (x > 0) {
binaryNumber[i] = x % 2;
x = x / 2;
i++;
}
for ( int j = 0; j < n - i; j++)
cout << '0' ;
for ( int j = i - 1; j >= 0; j--)
cout << binaryNumber[j];
}
void generateGrayarr( int n)
{
int N = 1 << n;
for ( int i = 0; i < N; i++) {
int x = i ^ (i >> 1);
decimalToBinaryNumber(x, n);
cout << endl;
}
}
int main()
{
int n = 3;
generateGrayarr(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void decimalToBinaryNumber( int x,
int n)
{
int [] binaryNumber = new int [x];
int i = 0 ;
while (x > 0 ) {
binaryNumber[i] = x % 2 ;
x = x / 2 ;
i++;
}
for ( int j = 0 ; j < n - i; j++)
System.out.print( '0' );
for ( int j = i - 1 ;
j >= 0 ; j--)
System.out.print(binaryNumber[j]);
}
static void generateGrayarr( int n)
{
int N = 1 << n;
for ( int i = 0 ; i < N; i++) {
int x = i ^ (i >> 1 );
decimalToBinaryNumber(x, n);
System.out.println();
}
}
public static void main(String[] args)
{
int n = 3 ;
generateGrayarr(n);
}
}
|
Python3
def decimalToBinaryNumber(x, n):
binaryNumber = [ 0 ] * x;
i = 0 ;
while (x > 0 ):
binaryNumber[i] = x % 2 ;
x = x / / 2 ;
i + = 1 ;
for j in range ( 0 , n - i):
print ( '0' , end = "");
for j in range (i - 1 , - 1 , - 1 ):
print (binaryNumber[j], end = "");
def generateGrayarr(n):
N = 1 << n;
for i in range (N):
x = i ^ (i >> 1 );
decimalToBinaryNumber(x, n);
print ();
if __name__ = = '__main__' :
n = 3 ;
generateGrayarr(n);
|
C#
using System;
class GFG {
static void decimalToBinaryNumber( int x,
int n)
{
int [] binaryNumber = new int [x];
int i = 0;
while (x > 0) {
binaryNumber[i] = x % 2;
x = x / 2;
i++;
}
for ( int j = 0; j < n - i; j++)
Console.Write( '0' );
for ( int j = i - 1;
j >= 0; j--)
Console.Write(binaryNumber[j]);
}
static void generateGrayarr( int n)
{
int N = 1 << n;
for ( int i = 0; i < N; i++) {
int x = i ^ (i >> 1);
decimalToBinaryNumber(x, n);
Console.WriteLine();
}
}
public static void Main()
{
int n = 3;
generateGrayarr(n);
}
}
|
Javascript
<script>
function decimalToBinaryNumber(x, n)
{
var binaryNumber = Array(x);
var i = 0;
while (x > 0) {
binaryNumber[i] = x % 2;
x = parseInt(x / 2);
i++;
}
for ( var j = 0; j < n - i; j++)
document.write( '0' );
for ( var j = i - 1; j >= 0; j--)
document.write( binaryNumber[j]);
}
function generateGrayarr(n)
{
var N = 1 << n;
for ( var i = 0; i < N; i++) {
var x = i ^ (i >> 1);
decimalToBinaryNumber(x, n);
document.write( "<br>" );
}
}
var n = 3;
generateGrayarr(n);
</script>
|
Output
000
001
011
010
110
111
101
100
Complexity Analysis:
- Time Complexity: O(2n).
Only one traversal from 0 to (2n) is needed.
- Auxiliary Space: O(log x).
A space of (log x) is required for binary representation of (x)
Share your thoughts in the comments
Please Login to comment...