
Input: 'n' = 5 , ‘arr’ = [3, 4, 5, 1, 2]
Output: 3
Explanation:
If we rotate the array [1 ,2, 3, 4, 5] right '3' times then we will get the 'arr'. Thus 'r' = 3.
The first line contains an integer ‘n’, representing the size of the array ‘arr’.
The second line contains ‘n’ integers, elements of ‘arr’.
Return an integer, value of ‘r’.
You don’t need to print anything, it has already been taken care of, just complete the given function.
Since we are rotating a sorted array ‘r’ times to the right, the minimum element would also be rotated ‘r’ times to the right. Thus we can perform a linear search to find the index of the minimum element and that would be our answer.
We can binary search on the array for the index of the minimum element. Let ‘low’ and ‘high’ represent the current range and ‘mid’ be the middle index of this range. If ‘arr[mid]’ > ‘arr[high]’ then the minimum value lies in the range ‘mid+1’ to ‘high’. Otherwise it lies in the range ‘low’ to ‘mid’.
Element Count in Ranges
First Digit One
Minimize Maximum Adjacent Distance
Sorted Doubly Linked List to Balanced BST
Minimized Maximum of Products Distributed to Any Store