Skip to content

Commit

Permalink
Time: 0 ms (100.00%), Space: 42.1 MB (99.31%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
saikatkar committed May 29, 2022
1 parent 810772b commit 5436be0
Showing 1 changed file with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public int peakIndexInMountainArray(int[] arr) {
return search(arr, 0, arr.length-1);
}

private int search(int[] arr, int start, int end) {
while (start <= end) {
int mid = start + (end -start)/2;
if (mid == 0) {
start = mid+1;
continue;
}
if (mid == arr.length-1) {
end = mid-1;
continue;
}
if (arr[mid] > arr[mid+1] && arr[mid] > arr[mid-1]) {
return mid;
}
if (arr[mid] > arr[mid+1] && arr[mid] < arr[mid-1]) {
end = mid-1;
} else if (arr[mid] > arr[mid-1] && arr[mid] < arr[mid+1]) {
start = mid+1;
}
}
return -1;
}
}

0 comments on commit 5436be0

Please sign in to comment.