-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeakIndexInAMountainArray.java
More file actions
37 lines (37 loc) · 1.36 KB
/
peakIndexInAMountainArray.java
File metadata and controls
37 lines (37 loc) · 1.36 KB
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
public class peakIndexInAMountainArray {
// https://leetcode.com/problems/peak-index-in-a-mountain-array/
//also called the bitonic array
public static void main(String[] args) {
int[] arr = {0, 2, 1, 0};
System.out.println(peakIndexInMountainArray(arr));
}
static int peakIndexInMountainArray(int[] arr)
{
int start = 0;
int end = arr.length-1;
while(start < end)
{
// int mid = (start + end)/2;
// this method is not efficient because start+end can sometimes exceed the limit of the integer range
int mid = start + (end - start)/2;
//this is the more efficient method
if(arr[mid] < arr[mid+1])
{
//you are in the asc part of the array
start = mid+1;
//because we know that mid+1 > mid
}
else
{
//you are in the desc part of the array
//this maybe the ans, but look at your left
//hence end != mid-1
end = mid;
}
}
//in the end start == end because of the checks above
//because the end and start are trying to find the largest element and
//when they are pointing to only one element then that will be the largest one only
return start;
}
}