-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKSum.java
More file actions
executable file
·61 lines (50 loc) · 1.68 KB
/
KSum.java
File metadata and controls
executable file
·61 lines (50 loc) · 1.68 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
/*
K Sum
*/
import java.util.*;
public class KSum{
public static void main(String[] args) {
int[] num = {-5,-4,-3,-2, 1, 3,3,5};
System.out.println(fourSum(num,-11));
//System.out.println(findSumFromSortArray(num,0,2,-2));
}
public static List<List<Integer>> findSumFromSortArray(int[] num, int bg, int count, int target){
List<List<Integer>> res = new ArrayList<List<Integer>>();
HashSet<Integer> visited = new HashSet<Integer>();
if(count == 2){
int i = bg, j = num.length - 1;
while(i < j){
int sum = num[i] + num[j];
if(sum == target && !visited.contains(num[i])
&& !visited.contains(num[j])){
List<Integer> tp = new ArrayList<Integer>(Arrays.asList(num[i],num[j]));
res.add(tp);
visited.add(num[i]);
visited.add(num[j]);
i++;
j--;
}else if (sum < target)
i++;
else
j--;
}
}else{
for(int i = bg; i < num.length; ++i){
if(visited.contains(num[i]))
continue;
visited.add(num[i]);
List<List<Integer>> subRes = findSumFromSortArray(num, i + 1, count - 1, target - num[i]);
if(subRes != null){
for(List<Integer> sb : subRes)
sb.add(0,num[i]);
res.addAll(subRes);
}
}
}
return res;
}
public static List<List<Integer>> fourSum(int[] num,int target){
Arrays.sort(num);
return findSumFromSortArray(num,0, 4, target);
}
}