Generate n | Java program to generate n - bit Gray codes ; This function generates all n bit Gray codes and prints the generated codes ; Base case ; Recursive case ; Append 0 to the first half ; Append 1 to the second half ; Function to generate the Gray code of N bits ; print contents of arr ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static ArrayList < String > generateGray ( int n ) { if ( n <= 0 ) { ArrayList < String > temp = new ArrayList < String > ( ) { { add ( "0" ) ; } } ; return temp ; } if ( n == 1 ) { ArrayList < String > temp = new ArrayList < String > ( ) { { add ( "0" ) ; add ( "1" ) ; } } ; return temp ; } ArrayList < String > recAns = generateGray ( n - 1 ) ; ArrayList < String > mainAns = new ArrayList < String > ( ) ; for ( int i = 0 ; i < recAns . size ( ) ; i ++ ) { String s = recAns . get ( i ) ; mainAns . add ( "0" + s ) ; } for ( int i = recAns . size ( ) - 1 ; i >= 0 ; i -- ) { String s = recAns . get ( i ) ; mainAns . add ( "1" + s ) ; } return mainAns ; } static void generateGrayarr ( int n ) { ArrayList < String > arr = new ArrayList < String > ( ) ; arr = generateGray ( n ) ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { System . out . println ( arr . get ( i ) ) ; } } public static void main ( String [ ] args ) { generateGrayarr ( 3 ) ; } }