-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.java
More file actions
50 lines (43 loc) · 963 Bytes
/
TwoSum.java
File metadata and controls
50 lines (43 loc) · 963 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
40
41
42
43
44
45
46
47
48
49
50
import java.util.*;
public class TwoSum {
//TODO : solve with subset sum
/**
* With using sort indices changes so make sure don't change indices
* @param sample
* @param target
* @return
*/
private int[] getTwoSum(int sample[],int target) {
/**
* cannot use List<int> why ?
* Complexity = O(nlogn)
*/
Arrays.sort(sample);
int low = 0;
int high = sample.length -1;
int sum = 0;
int [] indexList = new int[2];
while(low <= high) {
sum = sample[low] + sample[high];
if(sum == target) {
indexList[0] = low;
indexList[1] = high;
break;
}else if(sum < target) {
low ++;
}else {
high --;
}
}
return indexList;
}
public static void main(String args[]) {
TwoSum twosum = new TwoSum();
int [] sample = {150,24,79,50,88,345,3};
int target = 200;
int [] test = twosum.getTwoSum(sample, target);
for (int i=0;i<test.length;i++) {
System.out.print(test[i] + " ");
}
}
}