-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubsetSumList
More file actions
39 lines (32 loc) · 955 Bytes
/
SubsetSumList
File metadata and controls
39 lines (32 loc) · 955 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
package DynamicProgramming;
import java.util.ArrayList;
public class ResultList {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = {2, 3, 5, 6, 8, 10};
int[] result = new int[arr.length];
ArrayList<Integer> listy = new ArrayList<>();
getList(arr, arr.length, 10, listy, 0, 0);
}
private static void getList(int[] arr, int length, int sum, ArrayList<Integer> result, int index, int count) {
// TODO Auto-generated method stub
if(sum==0){
for(int i=0;i<result.size();i++) {
if(result.get(i) != 0)
System.out.print(result.get(i) +" ");
}
System.out.println();
result.clear();
return;
}
if(index >= length){
result.clear();
return;
}
ArrayList<Integer> target = new ArrayList();
target.addAll(result);
getList(arr, length, sum, target, index+1, count);
result.add(arr[index]);
getList(arr, length, sum - arr[index], result, index+1, count+1);
}
}