This is an interview question. A sorted rotated array is a sorted array which was rotated 0 or more times.
For example: [1,2,3] -> [2,3,1]
Please tell me what do you think about the following (correctness, efficiency, coding conventions) and specifically if I can remove in some way the special handle for array of two elements:
static int findMax(int arr[]) {
return findMax(arr, 0 , arr.length - 1);
}
static int findMax(int arr[], int low, int high) {
int middle;
if (low == high) {
return arr[low];
}
if ( Math.abs(high - low) == 1 ) {
return Math.max(arr[low], arr[high]);
}
middle = (low + high) >> 1;
if (arr[middle] > arr[middle + 1]) {
return arr[middle];
}
if (arr[low] > arr[middle]) {
return findMax(arr, low, middle - 1);
}
return findMax(arr, middle + 1, high);
}