FindNumberOfRotations

class Solution {
  public:
    int findKRotation(vector<int> &arr) {
        int low = 0;
        int high = arr.size()-1;
        int ans = arr.size(); 
        // the point at which array was rotated. // arr[x] > arr[x+1]
        if(arr[low] < arr[high])
            return 0;
        while(low <= high){
            int mid = low + (high-low)/2;
            if(mid < arr.size()-1 && arr[mid] > arr[mid+1])
                ans = mid+1;
            if(arr[low] <= arr[mid]) // L is sorted
            {
                low = mid+1;
            }else{
                high = mid-1;
            }
        }
        return ans;
    }
};
  • problem-link
  • Idea is to find the element where mid becomes greater than it's successor, that is the point at which it was rotated.
  • Problem was when to decide if the array was rotated n times/0 times. So we handle the 0 case initially.