-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsubsets_of_an_array.java
More file actions
51 lines (40 loc) · 887 Bytes
/
subsets_of_an_array.java
File metadata and controls
51 lines (40 loc) · 887 Bytes
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
import java.io.*;
import java.util.*;
public class subsets_of_an_array{
public static void main(String[] args) throws Exception {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int[] arr = new int[n];
for(int i = 0;i<arr.length;i++){
arr[i] = scn.nextInt();
}
int limit = (int)Math.pow(2, arr.length);
for(int i = 0; i < limit ; i++){
String set = "";
int temp = i;
// convert i to binary and use 0's and 1's to determine whether to print the number or not.
for(int j = arr.length - 1; j >= 0; j--){
int r = temp % 2;
temp = temp / 2;
if(r == 0){
set = "-\t" + set;
}else{
set = arr[j] + "\t" + set;
}
}
System.out.println(set);
}
}
}
/*
Input : 3 10 20 30
Output :
- - -
- - 30
- 20 -
- 20 30
10 - -
10 - 30
10 20 -
10 20 30
*/