-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPairSum1b.java
More file actions
35 lines (32 loc) · 836 Bytes
/
Copy pathPairSum1b.java
File metadata and controls
35 lines (32 loc) · 836 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
// 2 pointer approach
import java.util.ArrayList;
import java.util.*;
public class PairSum1b{
public static boolean isPresent(ArrayList<Integer> list, int target){
int lp=0;
int rp=list.size()-1;
while(lp != rp){
if(list.get(lp) + list.get(rp) == target){
return true;
}
else if(list.get(lp) + list.get(rp) < target){
lp++;
}
else{
rp--;
}
}
return false;
}
public static void main(String args[]){
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
int target = 5;
System.out.println(isPresent(list, target));
}
}