forked from nivedha-ravi/Code-and-Compile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary pattern.java
More file actions
83 lines (78 loc) · 1.62 KB
/
Binary pattern.java
File metadata and controls
83 lines (78 loc) · 1.62 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
Example Input/Output 1:
Input: 3
Output:
0
1
0 0
0 1
1 0
1 1
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
Explanation: Here N = 3.
All possible binary representations using 1 bit are 0 and 1.
All possible binary representations using 2 bits are 00, 01, 10 and 11.
All possible binary representations using 3 bits are 000, 001, 010, 011, 100, 101, 110 and 111.
Example Input/Output 2:
Input: 2
Output:
0
1
0 0
0 1
1 0
1 1
*/
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n=s.nextInt(),i,j,k;
String a="",b="";
for(i=1;i<=n;i++){
for(j=0;j<(int)(Math.pow(2,i));j++){
a=Integer.toBinaryString(j);
if(a.length()==i){
for(k=0;k<a.length();k++){
System.out.print(a.charAt(k)+" ");
}
}
else{
for(k=0;k<i-a.length();k++){
System.out.print("0 ");
}
for(k=0;k<a.length();k++){
System.out.print(a.charAt(k)+" ");
}
}
System.out.println();
}
}
}
}
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<String > arr= new ArrayList<>();
arr.add("0 ");
arr.add("1 ");
int n= sc.nextInt();
for(int i = 0 ; i < n ; i++){
List<String> temp = new ArrayList<>();
for(String str : arr){
temp.add(str+"0 ");
temp.add(str+"1 ");
System.out.println(str);
}
arr = temp;
}
}
}