package org.wesome.dsalgo;
public class RotationCount {
public static int rotationCount(int[] arr, int start, int end) {
if (start > end) {
return -1;
}
if (start == end) {
return -1;
}
int mid = (start + end) / 2;
//it's not an end and the mid element is greater than its next.
if (end > mid && arr[mid] > arr[mid + 1])
return (mid + 1);
//it's not start and the previous element is greater than mid.
if (mid > start && arr[mid - 1] > arr[mid])
return mid;
// if the end is greater than mid means it's sorted, rotation is on the left side.
if (arr[end] > arr[mid])
return rotationCount(arr, start, mid - 1);
// else rotation is in right side.
return rotationCount(arr, mid + 1, end);
}
}