Find Minimum in Rotated Sorted Array

MediumBinary Search · Rotated Array🧩 Binary SearchLeetCode #153 ↗Updated

Problem

An ascending array of distinct integers was rotated some unknown number of times (between 1 and n), so it may look like [4,5,6,7,0,1,2]. Given the rotated array nums, return its smallest element in O(log n) time.
Inputnums = [3,4,5,1,2]Output1
Inputnums = [4,5,6,7,0,1,2]Output0
Inputnums = [11,13,15,17]Output11 (rotated n times = unchanged)

1 ≤ n ≤ 5000  ·  -5000 ≤ nums[i] ≤ 5000  ·  all values distinct  ·  O(log n) required

Try it yourself

Java
class Solution {
    public int findMin(int[] nums) {
        
    }
}
Your code is saved in this browser.Submit on LeetCode ↗
Key insight
Compare nums[mid] with the last element of the window. If it is bigger, the “drop” (and so the minimum) is strictly to the right of mid; otherwise mid..hi is sorted and the minimum is at mid or to its left.
PatternBinary Search

Instinct Sorted data, a rotated array, or find the min/max X that works where a yes/no test flips only once - halve the search space every step.

Memory hack Find the fence - every candidate reads NO NO NO YES YES, and binary search just walks to where the fence stands.

Open the Binary Search guide →

Approaches

Brute ForceLinear Scan

Time O(n)Space O(1)

The minimum of any array can be found by looking at every element once and keeping the smallest. This never uses the fact that the array is two sorted runs glued together.

Java
public int findMin(int[] nums) {
    int min = nums[0];
    for (int x : nums) min = Math.min(min, x);
    return min;
}

Trivially correct, but O(n) breaks the logarithmic requirement.

At a glance

LevelApproachTimeSpace
Brute ForceLinear ScanO(n)O(1)
OptimalBinary Search Against the Right EndO(log n)O(1)
Common pitfalls
  • Comparing nums[mid] with nums[0] and mishandling an unrotated array.
  • Using hi = mid - 1 when mid itself might be the minimum.
  • Looping with lo <= hi and hi = mid — never terminates.
Interview tips
  • Draw the array as two ascending ramps with a cliff, then place mid on the picture.
  • Explain why every high-run value exceeds nums[hi] — that justifies each move.
  • Mention the duplicates follow-up (LC 154): when equal, shrink hi by one.
Pattern
Binary search on a monotone predicate → rotated sorted arrays, where “mid > right end” splits the array into two halves.
My notes saved in this browser