codebrew.ai

Find Minimum in Rotated Sorted Array

The minimum is the only element smaller than its predecessor — binary search the rotation point.

Approach

Compare nums[mid] to nums[hi]. If nums[mid] > nums[hi], the pivot (minimum) is to the right, so move lo up; otherwise it's at mid or to the left, so move hi down. Converge until lo == hi, which is the minimum.

Time complexity

O(log n)

Space complexity

O(1)

Common mistake

Comparing to nums[lo] instead of nums[hi] — the comparison to the right boundary is what stays correct under rotation.

See it run, step by step

Generate an interactive lesson for Find Minimum in Rotated Sorted Array — trace every variable and watch the algorithm execute until it clicks.

Start a lesson on Find Minimum in Rotated Sorted Array

Related problems