Skip to content

Commit b936036

Browse files
Create 39 40 41 45 46 MaxinBitonic.java
1 parent 908025f commit b936036

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package SearchingAlgorithms;
2+
3+
public class MaxinBitonic {
4+
5+
//O(logn) and O(1)
6+
public static int findMaximum(int[] arr){
7+
int n = arr.length;
8+
if(arr.length==1) return arr[0];
9+
if(arr.length==2) return Math.max(arr[0], arr[1]);
10+
int low = 0, high = n-1;
11+
while(low<high){
12+
int mid = low+(high-low)/2;
13+
if(arr[mid]>arr[mid-1] && arr[mid]>arr[mid+1]) return arr[mid];
14+
else if(arr[mid]>arr[mid-1] && arr[mid+1]>arr[mid]) low = mid+1;
15+
else high = mid-1;
16+
}
17+
return arr[high];
18+
}
19+
20+
public static void main (String[] args)
21+
{
22+
int arr[] = {1, 3, 50, 10, 9, 7, 6};
23+
System.out.println("The maximum element is "+
24+
findMaximum(arr));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)