Find Kth Rotation

easy

You are given an array arr of distinct integers that was originally sorted in strictly increasing order, and then right-rotated k times. Your task is to find and return k — the number of rotations.

A single right rotation moves the last element to the front. Starting from [2, 4, 6, 9]:

  • After the 1st rotation: [9, 2, 4, 6]
  • After the 2nd rotation: [6, 9, 2, 4]

If the array was not rotated at all, return 0.

Hints

Picture the original sorted array. After one right rotation, where does the largest element go — and where does the smallest end up after k rotations?
The rotation count is exactly the index of one special element. Which one, and why?
The array is two sorted runs glued at a single drop. Compare arr[mid] with arr[hi]: if arr[mid] is bigger, the drop is to the right; otherwise it is at mid or to the left. That one question halves the window — binary search it in O(log n).

Common doubts

Each right rotation moves the last element to the front. After k rotations, the last k elements of the sorted array occupy indices 0 .. k-1, so the original first element — the minimum — sits exactly at index k.
When the current window is already sorted (no drop inside it), arr[mid] < arr[hi] correctly says the minimum is at mid or to the left. Comparing against arr[lo] is ambiguous in that case — arr[mid] > arr[lo] holds both in a sorted window and when the drop is to the right, so it cannot tell the two apart.
The minimum is at index 0. The binary search naturally converges there: arr[mid] > arr[hi] is never true in a sorted array, so hi keeps shrinking to 0. No special case needed.
No. Floor division guarantees mid < hi whenever lo < hi, so hi = mid strictly decreases hi, and lo = mid + 1 strictly increases lo. The window shrinks every iteration.

Interview follow-ups

Ties break the halving argument: when arr[mid] == arr[hi] you cannot tell which side holds the drop, so you shrink conservatively with hi -= 1. The algorithm stays correct but degrades to O(n) in the worst case (e.g. all elements equal).
Two ways: find the pivot with this exact algorithm, then run a standard binary search in the correct sorted half — or use a single modified binary search that first determines which half of the window is sorted and checks whether the target lies inside it.
Reverse the first k elements, reverse the remaining n - k, then reverse the whole array — the classic three-reversal rotation trick, in O(n) time and O(1) space.

Fun facts

  • Rotating right k times is the same as rotating left n - k times — which is why the rotation count is always taken modulo n, and rotating n times lands you back where you started.
  • This find-the-pivot template is the warm-up for a whole family of problems — Search in Rotated Sorted Array and Minimum in Rotated Sorted Array are the same cliff hunt with one extra step bolted on.
  • The same drop-detection idea shows up in systems work: circular buffers and log-structured storage locate their oldest entry by finding exactly this kind of wrap-around point.

Asked at

AmazonMicrosoftAdobeOracle
Frequently Sometimes Occasionally
Example 1
Input: arr = [5, 1, 2, 3, 4]
Output: 1
The original sorted array is [1, 2, 3, 4, 5]. One right rotation moves 5 to the front, producing [5, 1, 2, 3, 4], so k = 1.
Example 2
Input: arr = [1, 2, 3, 4, 5]
Output: 0
The array is already sorted, so it was rotated 0 times.
Constraints

- 1 <= arr.size <= 10^5 - 1 <= arr[i] <= 10^7 - All elements of arr are distinct - arr is a sorted array rotated right k times, 0 <= k < arr.size

Solve this problem →