-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathPeakIndexInMountainArray.java
38 lines (32 loc) · 1.24 KB
/
PeakIndexInMountainArray.java
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
package binarysearch.problems;
import utils.Reader;
import utils.Helper;
import java.io.IOException;
// Problem link: https://leetcode.com/problems/peak-index-in-a-mountain-array/description/
class PeakIndexInMountainArray {
public static void main (String[] args) throws IOException {
Reader reader = new Reader();
int n = reader.nextInt();
int[] arr = Helper.getIntArray(n, reader);
PeakIndexInMountainArray obj = new PeakIndexInMountainArray();
System.out.println(obj.peakIndexInMountainArray(arr));
}
public int peakIndexInMountainArray(int[] arr) {
return findPeakIndex(arr, 0, arr.length-1);
}
public int findPeakIndex(int[] arr, int start, int end) {
if (start <= end) {
int mid = (start + end)/2;
if (((mid-1) >= 0) && (arr[mid-1] < arr[mid]) && ((mid+1) < arr.length) && (arr[mid+1] < arr[mid])) {
return mid;
}
if (((mid+1) < arr.length) && (arr[mid] < arr[mid+1])) {
return findPeakIndex(arr, mid+1, end);
}
if (((mid+1) < arr.length) && (arr[mid] > arr[mid+1])) {
return findPeakIndex(arr, start, mid-1);
}
}
return -1;
}
}