Created
July 17, 2019 06:28
-
-
Save a947/dde7049bee56752a3e605b5005bb4ee9 to your computer and use it in GitHub Desktop.
Bitonic Array Maximum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MaxInBitonicArray { | |
public static int findMax(int[] arr) { | |
int start = 0, end = arr.length - 1; | |
while (start < end) { | |
int mid = start + (end - start) / 2; | |
if (arr[mid] > arr[mid + 1]) { | |
end = mid; | |
} else { | |
start = mid + 1; | |
} | |
} | |
// at the end of the while loop, 'start == end' | |
return arr[start]; | |
} | |
public static void main(String[] args) { | |
System.out.println(MaxInBitonicArray.findMax(new int[] { 1, 3, 8, 12, 4, 2 })); | |
System.out.println(MaxInBitonicArray.findMax(new int[] { 3, 8, 3, 1 })); | |
System.out.println(MaxInBitonicArray.findMax(new int[] { 1, 3, 8, 12 })); | |
System.out.println(MaxInBitonicArray.findMax(new int[] { 10, 9, 8 })); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment