Find Minimum in Rotated Sorted Array

medium

A sorted array of unique integers has been rotated somewhere between 1 and n times. Rotating once moves the last element to the front: [a[0], a[1], ..., a[n-1]] becomes [a[n-1], a[0], a[1], ..., a[n-2]].

For example, [0,1,2,4,5,6,7] might appear as:

  • [4,5,6,7,0,1,2] after 4 rotations.
  • [0,1,2,4,5,6,7] after 7 rotations (a full cycle lands it back where it started).

Given the rotated array nums, return the minimum element.

A linear scan is easy — your algorithm must run in O(log n) time.

Hints

Picture the values as a hiking trail: they climb, fall off a single cliff, then climb again. Where does the smallest element live relative to that cliff?
You do not need to inspect every element. Pick the middle one and compare it with the last element — what does each outcome tell you about which half contains the cliff?
Binary search on the range, not a target: if nums[mid] > nums[hi], the minimum is strictly right of mid (lo = mid + 1); otherwise it is at mid or left (hi = mid). Stop when lo == hi.

Common doubts

The left comparison is ambiguous. In [1,2,3,4,5] (fully rotated, so sorted) and in [3,4,5,1,2], nums[mid] > nums[lo] holds in both — yet the minimum is on opposite sides. nums[hi] gives an unambiguous signal: nums[mid] > nums[hi] means the drop is right of mid, otherwise it is at mid or left.
When nums[mid] < nums[hi], index mid might itself hold the minimum — you have not ruled it out. hi = mid - 1 could step past the answer. The asymmetry is safe: in the other branch nums[mid] > nums[hi] proves mid is not the minimum, so lo = mid + 1 is fine.
It lands back fully sorted, e.g. [11,13,15,17]. Then nums[mid] < nums[hi] on every step, hi walks down to 0, and the algorithm correctly returns nums[0] — no special case needed.
Yes. With lo == hi == 0 the loop never runs and nums[0] is returned immediately.

Interview follow-ups

The comparison nums[mid] == nums[hi] becomes ambiguous — you cannot tell which side the drop is on. The fix is to shrink conservatively: hi -= 1 on ties. Worst case (all equal values) degrades to O(n), which is provably unavoidable.
Yes, with zero extra work — the rotation count equals the index of the minimum. Return lo instead of nums[lo].
Same compare-with-the-endpoint idea: at each step one half of [lo, hi] is fully sorted — check whether the target lies inside that sorted half, and recurse into the correct side. That is exactly Search in Rotated Sorted Array.

Fun facts

  • The index of the minimum is the rotation count — so solving this problem silently answers a second interview question for free.
  • The compare-with-the-right-end trick is the master key to the whole rotated-array family: finding a target, handling duplicates, and counting rotations all reuse the exact same comparison.
  • This is binary search with no target at all — a gateway to binary search on the answer, the pattern behind problems like allocating books or shipping packages within D days.

Asked at

AmazonMicrosoftGoogleGoldman SachsAdobe
Frequently Sometimes Occasionally
Example 1
Input: nums = [3,4,5,1,2]
Output: 1
The original array was [1,2,3,4,5], rotated 3 times. The minimum is 1.
Example 2
Input: nums = [4,5,6,7,0,1,2]
Output: 0
The original array was [0,1,2,4,5,6,7], rotated 4 times. The minimum is 0.
Example 3
Input: nums = [11,13,15,17]
Output: 11
Rotated 4 times — a full cycle — so the array is still fully sorted and the minimum is the first element.
Constraints

- n == nums.length - 1 <= n <= 5000 - -5000 <= nums[i] <= 5000 - All the integers of nums are unique. - nums is sorted and rotated between 1 and n times.

Solve this problem →