-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeakElement.java
More file actions
28 lines (25 loc) · 791 Bytes
/
PeakElement.java
File metadata and controls
28 lines (25 loc) · 791 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
public class PeakElement {
public static int peak(int a[], int l, int h) {
if(l<=h) {
int mid = l + (h-l)/2;
if(mid == a.length -1){
if(a[mid] > a[mid-1]) return mid;
else return peak(a,l,mid-1);
}
else if(mid == 0){
if(a[mid]>a[mid+1]) return mid;
else return peak(a,mid+1, h);
}
else {
if(a[mid] > a[mid+1] && a[mid]>a[mid-1]) return mid;
else if(a[mid]<a[mid-1]) return peak(a,l,mid-1);
return peak(a,mid+1,h);
}
}
return -1;
}
public static void main(String args[]) {
int a[] = {2,1};
System.out.println(peak(a, 0,a.length-1));
}
}