-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindPeak.java
More file actions
39 lines (25 loc) · 722 Bytes
/
FindPeak.java
File metadata and controls
39 lines (25 loc) · 722 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
33
34
35
36
37
38
39
package com.vinay.practice.lc;
// https://leetcode.com/problems/peak-index-in-a-mountain-array/
// https://leetcode.com/problems/find-peak-element/
public class FindPeak {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = {0,1,4,10,11,5,2};
//int ele = 10;
//int end = findLimit(arr, ele);
System.out.println("The peak element found is : " + arr[findPeak(arr)]);
}
public static int findPeak(int arr[]) {
int start = 0;
int end = arr.length -1;
while(start < end) {
int mid = start + (end - start)/2;
if(arr[mid] > arr[mid+1]) {
end = mid;
} else if(arr[mid] < arr[mid+1]) {
start = mid+1;
}
}
return start;
}
}