AlgoInsight

DSA problem

Find Minimum in Rotated Sorted Array

MediumBinary SearchPattern: Binary Searchtop300AmazonMicrosoftGoogleFacebookApple

Suppose an array of length n sorted in ascending order is rotated between 1 and n times. Given the sorted rotated array nums of unique elements, return the minimum element of this array. You must write an algorithm that runs in O(log n) time.

A rotated sorted array can be visualized as two ascending segments separated by a "cliff": every element on the left of the cliff is greater than every element on the right. The minimum sits exactly at the start of the right segment. Binary search efficiently finds that boundary.

The clean comparison to use is nums[mid] vs nums[hi] (not nums[lo]). Because the right segment is purely ascending up to nums[hi], the test nums[mid] > nums[hi] reveals everything: if true, the cliff is strictly to the right of mid, so move lo = mid + 1; if false, then mid is in (or at the start of) the right segment, so the minimum is at or before mid — narrow hi = mid.

Use the lower-bound shape while (lo < hi). When the loop terminates, lo == hi is the index of the minimum. The mid + 1 / hi = mid update structure guarantees the window shrinks each iteration without skipping the answer. The whole algorithm is O(log n) time and O(1) space.

Edge cases that the comparison handles naturally: a non-rotated array (the cliff is at index 0, nums[mid] <= nums[hi] immediately holds), a fully rotated array, and arrays of length 1 or 2.

Examples

Example 1
Input: nums = [3,4,5,1,2]
Output: 1
Explanation: The cliff is between 5 and 1. Binary search converges on index 3 (value 1) in O(log n) steps.
Example 2
Input: nums = [4,5,6,7,0,1,2]
Output: 0
Explanation: The cliff is between 7 and 0; the minimum is 0 at index 4.
Example 3
Input: nums = [11,13,15,17]
Output: 11
Explanation: A non-rotated array — the comparison nums[mid] > nums[hi] is false on the very first iteration, narrowing hi all the way down to index 0.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

binary-searcharrayrotated-array

Browse all DSA problems