-
Notifications
You must be signed in to change notification settings - Fork 6
/
SubSetsofSet.java
49 lines (33 loc) · 980 Bytes
/
SubSetsofSet.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
46
47
48
49
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SubSetsofSet
{
private List<List<Integer>> subsetsAgain(List<Integer> list, int index){
List<List<Integer>> allSubsets = null;
if(list.size() == index){
allSubsets = new ArrayList<>();
allSubsets.add(new ArrayList<Integer>());
return allSubsets;
}
int value = list.get(index);
allSubsets = subsetsAgain(list, index+1);
List<List<Integer>> output = new ArrayList<>();
for (List<Integer> list2 : allSubsets)
{
List<Integer> newsubset = new ArrayList<>();
newsubset.addAll(list2);
newsubset.add(value);
output.add(newsubset);
}
allSubsets.addAll(output);
return allSubsets;
}
public static void main(String[] args)
{
List<List<Integer>> finaloutput = new SubSetsofSet().subsetsAgain(Arrays.asList(1, 2, 3,4), 0);
for (List<Integer> subsets : finaloutput) {
System.out.println(subsets);
}
}
}