-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathSubsetsN.java
48 lines (40 loc) · 1.23 KB
/
SubsetsN.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
import java.util.HashSet;
import java.util.ArrayList;
// Given an array of integers, return all possible subsets of length n
public class SubsetsN {
int[] arr = {2,3,5,7,4,3,22,4,6,7,889,4,3,2,4,6,7,8,4,34};
Integer[] newArr;
// remove duplicates in array O(N)
void preprocess(){
HashSet<Integer> map = new HashSet<Integer>();
ArrayList<Integer> temp = new ArrayList<Integer>();
for(int i=0; i<arr.length; i++){
int curr = arr[i];
if (map.contains(curr)) continue;
else{
temp.add(curr);
map.add(curr);
}
}
newArr = temp.toArray(new Integer[temp.size()]);
}
void getAllSubsets(int start, int length, ArrayList<Integer> list){
// base case
if (length == 0){
System.out.println(list.toString()); // prints the subset
return;
}
for (int i=start; i + (length-1) < newArr.length - length + 1 ; i++){
ArrayList<Integer> newList = new ArrayList<Integer>(list); // create new list for thread safety
newList.add(newArr[i]); // add current item
getAllSubsets(i + 1, length - 1, newList); // recurse with current item added
}
}
void run(){
preprocess();
getAllSubsets(0, 5, new ArrayList<Integer>());
}
public static void main(String[] args) {
(new SubsetsN()).run();
}
}