-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsubsetSum1.java
More file actions
33 lines (26 loc) · 877 Bytes
/
subsetSum1.java
File metadata and controls
33 lines (26 loc) · 877 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
import java.util.*;
public class subsetSum1 {
public static void subset(int index, int[] arr, int sum, ArrayList<Integer> ds, ArrayList<Integer> sumSets) {
if (index == arr.length) {
sumSets.add(sum);
return;
}
// condition to pick
ds.add(arr[index]);
subset(index + 1, arr, sum + arr[index], ds, sumSets);
ds.remove(ds.size() - 1);
// condition to not pick
subset(index + 1, arr, sum, ds, sumSets);
}
public static void makeSums(int[] arr) {
ArrayList<Integer> ds = new ArrayList<>();
ArrayList<Integer> sumSets = new ArrayList<>();
subset(0, arr, 0, ds, sumSets);
sumSets.sort(null);
System.out.println(sumSets);
}
public static void main(String[] args) {
int[] arr = { 3, 1, 2 };
makeSums(arr);
}
}