2Brute2Best
πŸ”₯0

Find Minimum in Rotated Sorted Array

MediumΒ·LeetCode #153Basic Anchor
LeetCode
30-Second Mental ModelBinary Search (rotated array β€” find the pivot)
Complexity:O(N)β†’O(log N)
Key Insight

The array is a high run followed by a low run, and the minimum is where the low run starts (the "drop"). Compare nums[mid] with nums[hi]. If nums[mid] > nums[hi], mid is in the high run, so the drop is to the right: lo = mid + 1. Otherwise mid is already in the low run (or the array is sorted), so the minimum is at mid or to its left: hi = mid. Each step halves the range.

Core Validity Invariant

Within [lo, hi], nums[mid] > nums[hi] is true exactly when mid is before the drop. So the minimum is right of mid when it is true, and at mid or left of it when it is false.

01

Problem

Someone took a sorted array of distinct numbers (smallest to largest) and rotated it: they cut it at some point and moved the front part to the back. For example, [0, 1, 2, 4, 5, 6, 7] can become [4, 5, 6, 7, 0, 1, 2]. They may also have rotated it by 0 steps, so it can still be fully sorted.

You get the rotated array nums. Return its smallest element. Your solution must run in O(log n) time.

Input

An integer array nums: a sorted array of distinct values, rotated 0 to n βˆ’ 1 times.

Output

The minimum value in nums.

Example 1
Input: nums = [3, 4, 5, 1, 2]
Output: 1

The original array was [1, 2, 3, 4, 5], rotated 3 times. The values climb 3, 4, 5, then drop to 1. The value right after the drop is the minimum.

Example 2
Input: nums = [4, 5, 6, 7, 0, 1, 2]
Output: 0

The drop is from 7 to 0, so 0 is the minimum.

Example 3
Input: nums = [11, 13, 15, 17]
Output: 11

Rotated 4 times (a full circle), so it is still sorted. There is no drop, and the minimum is the first element.

Constraints
  • β€’ 1 ≀ n ≀ 5000
  • β€’ βˆ’5000 ≀ nums[i] ≀ 5000
  • β€’ All values are distinct.
  • β€’ nums is a sorted array rotated between 1 and n times (rotating n times gives back the sorted array).
02

First, think yourself

Before opening any solution, try to answer these on paper. Use hints only when stuck β€” each hint is counted.

  1. 1.

    If you draw the values as a line graph, what shape does a rotated sorted array make?

  2. 2.

    Where exactly is the minimum in that shape?

  3. 3.

    If I pick a middle element, can I tell whether it is in the 'high' part or the 'low' part of the array?

  4. 4.

    Which element should I compare nums[mid] with to answer that β€” and does it still work if the array was not rotated at all?

  5. 5.

    Could mid itself be the minimum? What does that mean for how I move hi?

03

Brute force approach

05

Optimized approach

06

Why does this algorithm work?

07

Dry run

08

All approaches compared

ApproachIdeaTimeSpace
Brute ForceWalk the whole array and keep the smallest valueO(N)O(1)
OptimalCompare nums[mid] with nums[hi]: bigger β†’ go right, else keep mid and go leftO(log N)O(1)
09

Why does this belong to this pattern?

Clues in the problem statement
  • β€œsorted array, then rotated”Not fully sorted, but made of two sorted runs. Binary search still works if one check tells you which run mid is in.
  • β€œfind the minimum”There is no target. You are looking for a position β€” the drop β€” not a value.
  • β€œO(log n)”A linear scan is ruled out. Halve the range each step.
  • β€œdistinct values”`nums[mid] == nums[hi]` only happens when mid == hi, so the two-way check is always clear.
Rotated sorted array+find the drop (no target)+compare mid with hi→Binary Search (rotated array — find the pivot)

This is the anchor of the rotated sub-pattern. The key idea: binary search does not need a target, it needs a yes/no question at mid that tells you which half to keep. Here the question is "is nums[mid] > nums[hi]?". Search in Rotated Sorted Array builds on this (find which half is sorted, then check the target), and Find Peak Element uses the same "which direction must the answer be in?" idea with a slope instead of a drop.

10

Don't make this mistake

Comparing with nums[lo] instead of nums[hi]
βœ— if (nums[mid] > nums[lo]) lo = mid + 1;
βœ“ if (nums[mid] > nums[hi]) lo = mid + 1;

For an array that was not rotated, like [1, 2, 3, 4, 5], nums[mid] > nums[lo] is true, so you move right and lose the minimum at index 0. nums[hi] is always in the low run, so comparing with it works for every rotation, including 0.

Using hi = mid βˆ’ 1
βœ— else hi = mid - 1;
βœ“ else hi = mid;

When nums[mid] < nums[hi], mid itself might be the minimum. In [4, 5, 1, 2, 3], mid = 2 points at 1, the answer. hi = mid βˆ’ 1 throws it away. This loop is while (lo < hi) with hi = mid; don't mix it with the lo <= hi / hi = mid βˆ’ 1 style.

Using while (lo <= hi) with hi = mid
βœ— while (lo <= hi) { ... hi = mid; }
βœ“ while (lo < hi) { ... hi = mid; }

When lo == hi, mid == hi and the else branch sets hi = mid again. Nothing changes, so the loop runs forever.

Pattern confusion: searching for a target

There is no target here, so there is nothing to compare nums[mid] with except the array itself. You are looking for the drop β€” the one place where a value is smaller than the one before it. If you catch yourself writing if (nums[mid] == target), you are solving Search in Rotated Sorted Array instead.

11

Want to implement this?

Write it yourself on LeetCode, then come back and compare your code with the solutions above.

Open coding workspace
How did it go?
Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Binary Search translate into production architectures and low-level memory mechanics.