-
Notifications
You must be signed in to change notification settings - Fork 0
/
ARRAYLIST9.java
46 lines (38 loc) · 1.11 KB
/
ARRAYLIST9.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
37
38
39
40
41
42
43
44
45
46
import java.util.ArrayList;
public class ARRAYLIST9 {
public static boolean TargetSum(ArrayList<Integer> list, int target) {
int n = list.size();
int bi = -1;
for (int i = 0; i < list.size() - 1; i++) {
if (list.get(i) > list.get(i + 1)) {
bi = i + 1;
break;
}
}
int start = bi;
int end = bi - 1;
while (start != end) {
if (list.get(start) + list.get(end) == target) {
return true;
}
if (list.get(start) + list.get(end) < target) {
start = (start + 1) % n;
} else {
end = (end + n - 1) % n;
}
}
return false;
}
public static void main(String[] args) {
// PAIR SUM IN A SORTED AND ROTATED ARRAYLIST
ArrayList<Integer> list = new ArrayList<>();
list.add(11);
list.add(15);
list.add(6);
list.add(8);
list.add(9);
list.add(10);
int targetsum = 50;
System.out.println(TargetSum(list, targetsum));
}
}