-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathBitonicArray.java
More file actions
32 lines (26 loc) · 825 Bytes
/
BitonicArray.java
File metadata and controls
32 lines (26 loc) · 825 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
//Bitonic Array or Mountain array
// find the greatest element in the array that is first gradually increasing and then decreasting like a mountain
// {1,3,6,9,15,14,8,7,2,0}; here ans is 15
public class BitonicArray
{
public static void main(String[] args) {
int arr[] = { 1, 3, 6, 9, 17, 19, 20, 29, 15, 14, 8, 7, 2, 0 };
int ans = search(arr);
System.out.println(ans);
}
static int search(int[] arr) {
int start = 0;
int end = arr.length - 1;
int great = 0;
while (start <= end) {
int mid = start + (end - start) / 2;
if (arr[mid] > arr[mid + 1]) {
great = arr[mid];
end = mid - 1;
} else {
start = mid + 1;
}
}
return great;
}
}