-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_0852.java
More file actions
22 lines (21 loc) · 724 Bytes
/
_0852.java
File metadata and controls
22 lines (21 loc) · 724 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.github.aditya;
public class _0852 {
// 0 ms, faster than 100.00%, memory 38.7 MB, less than 97.49%, Binary Search 0(log n)
class Solution {
public int peakIndexInMountainArray(int[] arr) {
int left = 0;
int right = arr.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] > arr[mid + 1] && arr[mid] > arr[mid - 1]) {
return mid;
} else if (arr[mid] > arr[mid + 1]) {
right = mid;
} else if (arr[mid] > arr[mid - 1]) {
left = mid;
}
}
return -1;
}
}
}