-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.java
More file actions
32 lines (29 loc) · 859 Bytes
/
Copy pathTwoSum.java
File metadata and controls
32 lines (29 loc) · 859 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
import java.util.Arrays;
public class TwoSum {
public static void main(String[] args) {
int nums[] = {-4,1,-2,6,2,3,1,4,5,5};
int target = 9;
Arrays.sort(nums);
int n= nums.length;
int left= 0 , right = n-1;
while(left<right){
int sum= nums[left]+nums[right];
if(sum==target){
System.out.println(left+" "+right);
left++;
right--;
while(left>0 && left<right && nums[left]==nums[left-1]){
left++;
}
while(right<n-1 && left<right && nums[right]==nums[right+1]){
right--;
}
}
else if(sum<target){
left++;
}
else{
right--;
}
}
}}