-
Notifications
You must be signed in to change notification settings - Fork 0
/
UniqueSumCombination.java
36 lines (28 loc) · 1.17 KB
/
UniqueSumCombination.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
import java.util.*;
public class UniqueSumCombination {
public static void main(String args[]) {
int[] arr = {1,1,2,5,6,7,10};
int sum = 8;
ArrayList<Integer> tempResult = new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
findCombinations(0,arr,0,sum,tempResult,result);
System.out.println(result);
}
private static void findCombinations(int currIndex,int[] arr, int currSum, int actualSum, ArrayList<Integer> tempResult, ArrayList<ArrayList<Integer>> result) {
if(actualSum == currSum) {
result.add(new ArrayList<Integer>(tempResult));
return;
}
for(int i=currIndex; i<arr.length; i++) {
if(currSum+arr[i] > actualSum){
break;
}
if(i>currIndex && arr[i-1] == arr[i]){
continue;
}
tempResult.add(arr[i]);
findCombinations(i+1,arr,arr[i]+currSum,actualSum,tempResult,result);
tempResult.remove(tempResult.size()-1);
}
}
}