-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ8_2205.java
45 lines (32 loc) · 1.02 KB
/
Q8_2205.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
* Name: Ishan Kumar
* Reg no : 2341011165
* PS link: https://cses.fi/paste/643ef699f5b2e7ea70b6c9/
*/
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
getGrayCode(n);
}
private static void getGrayCode(int n) {
if (n <= 0) {
return;
}
String[] grayCode = new String[]{"0", "1"};
for (int i = 2; i <= n; i++) {
String[] newGrayCode = new String[2 * grayCode.length];
for (int j = 0; j < grayCode.length; j++) {
newGrayCode[j] = "0" + grayCode[j];
}
for (int j = grayCode.length - 1, k = 0; j >= 0; j--, k++) {
newGrayCode[k + grayCode.length] = "1" + grayCode[j];
}
grayCode = newGrayCode;
}
for (String code : grayCode) {
System.out.println(code);
}
}
}