-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrint All Subsets - Iteration.java
More file actions
49 lines (42 loc) · 1.12 KB
/
Copy pathPrint All Subsets - Iteration.java
File metadata and controls
49 lines (42 loc) · 1.12 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
import java.io.*;
import java.util.*;
public class Main{
public static int decimalToBinary(int dec)
{
int bin = 0, power = 1;
while(dec > 0)
{
int dig = dec % 2;
dec = dec / 2;
bin = bin + dig * power;
power = power * 10;
}
return bin;
}
public static void main(String[] args) throws Exception {
// write your code here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[] a = new int[n];
for(int i = 0; i < n; i++){
a[i] = Integer.parseInt(br.readLine());
}
int totalSubsets = (int)Math.pow(2, n);
for(int dec=0; dec<totalSubsets; dec++)
{
int binaryNo = decimalToBinary(dec);
// System.out.println(binaryNo);
// Subset
int div = (int)Math.pow(10, n-1);
for(int i=0; i<n; i++)
{
int quot = binaryNo / div;
if(quot % 10 == 0)
System.out.print("- ");
else System.out.print(a[i] + " ");
div = div / 10;
}
System.out.println();
}
}
}