Skip to content

Instantly share code, notes, and snippets.

@a947
Created July 17, 2019 06:28
Show Gist options
  • Save a947/dde7049bee56752a3e605b5005bb4ee9 to your computer and use it in GitHub Desktop.
Save a947/dde7049bee56752a3e605b5005bb4ee9 to your computer and use it in GitHub Desktop.
Bitonic Array Maximum
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