-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitonic point
49 lines (40 loc) · 1.22 KB
/
Bitonic point
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
//Bitonic point
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int tc = Integer.parseInt(br.readLine().trim());
while(tc-- > 0) {
String[] str = br.readLine().trim().split(" ");
int[] arr = new int[str.length];
for(int i = 0; i < str.length; i++) {
arr[i] = Integer.parseInt(str[i]);
}
int ans = new Solution().findMaximum(arr);
System.out.println(ans);
System.out.println("~");
}
}
}
class Solution {
public int findMaximum(int[] arr) {
int n = arr.length;
int low = 0, high = n - 1;
int ind = 0;
while(low <= high) {
int mid = low + (high - low) / 2;
if(arr[mid] > arr[mid - 1] && arr[mid] > arr[mid + 1]) {
ind = mid;
break;
}
else if(arr[mid] < arr[mid + 1]) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
return arr[ind];
}
}