Why Find Minimum in Rotated Sorted Array Matters
Find Minimum in Rotated Sorted Array (LeetCode #153) is one of the most important binary search problems you will encounter in coding interviews. It teaches you how to apply binary search when the array is not fully sorted — a skill that directly transfers to harder problems like Search in Rotated Sorted Array (#33) and Search in Rotated Sorted Array II (#81).
The problem is deceptively simple: given a sorted array that has been rotated at some unknown pivot, find the minimum element in O(log n) time. A linear scan works, but the interviewer wants to see binary search. The real lesson is learning which half of the array to eliminate at each step when the usual sorted-order guarantee is broken.
Once you understand the compare-mid-to-right technique in this problem, you have the foundation for every rotated array question. This walkthrough breaks down the intuition, the code, the edge cases, and why comparing to the right boundary — not the left — is the key insight.
Understanding the Problem
You are given an array of unique integers that was originally sorted in ascending order, then rotated between 1 and n times. For example, [1,2,3,4,5] rotated 3 times becomes [3,4,5,1,2]. Your task is to find the minimum element, and the solution must run in O(log n) time.
The rotation creates two sorted subarrays. In [3,4,5,1,2], the left subarray [3,4,5] and the right subarray [1,2] are each individually sorted. The minimum element sits at the boundary between these two halves — it is the first element of the right subarray, also called the rotation point or pivot.
The O(log n) requirement rules out a simple linear scan. You need binary search, but standard binary search assumes a fully sorted array. The challenge is adapting binary search to work when the array has this rotated structure, where one half is sorted and the other contains the pivot.
Prerequisite Pattern
Find Minimum in Rotated Sorted Array (#153) is the prerequisite for Search in Rotated Sorted Array (#33) — master #153 first, then #33 adds a target search on top.
The Binary Search Approach for Rotated Arrays
The core insight for this find minimum rotated sorted array leetcode problem is simple: compare nums[mid] to nums[right]. This single comparison tells you which half of the array contains the minimum.
If nums[mid] is greater than nums[right], the minimum must be in the right half. Why? Because a value larger than the rightmost element means the rotation point (where values drop) is somewhere between mid and right. So you move left to mid + 1.
If nums[mid] is less than or equal to nums[right], the minimum is in the left half — including mid itself. The subarray from mid to right is already sorted, so the minimum cannot be hiding there. You move right to mid, keeping mid as a candidate.
You continue narrowing the window until left equals right. At that point, nums[left] is the minimum. The algorithm runs in O(log n) time because you cut the search space in half at each step, just like standard binary search.
- If nums[mid] > nums[right]: minimum is in the right half, set left = mid + 1
- If nums[mid] <= nums[right]: minimum is in the left half (including mid), set right = mid
- Terminate when left == right: nums[left] is the answer
- Time complexity: O(log n) — space complexity: O(1)
Why Compare to Right, Not Left
A common question is: why compare nums[mid] to nums[right] instead of nums[left]? The answer reveals a subtle but critical property of rotated arrays that trips up many candidates.
Consider the array [1,2,3,4,5] — a sorted array with no rotation. If you compare nums[mid] to nums[left], you find nums[mid] (3) >= nums[left] (1), which suggests the left half is sorted and the minimum is on the right. But the minimum is actually at index 0. Comparing to left gives you the wrong signal when the array is not rotated.
Comparing to nums[right] works in all cases. In a non-rotated array [1,2,3,4,5], nums[mid] (3) <= nums[right] (5), so you correctly move right to mid, eventually finding the minimum at index 0. In a rotated array [3,4,5,1,2], nums[mid] (5) > nums[right] (2), so you correctly move left to mid + 1, narrowing toward the minimum.
This is not just a LeetCode trick — it reflects a real property. The right boundary always belongs to the same sorted subarray as the minimum (or is the minimum itself), making it a reliable comparison anchor. The left boundary can belong to either subarray, making it unreliable.
Pro Tip
Always compare nums[mid] to nums[right], not nums[left] — comparing to left fails when the array isn't rotated (e.g., [1,2,3,4,5]). Comparing to right works in all cases.
Visual Walkthrough: Step-by-Step Examples
Let us trace through two examples to see the rotated array binary search in action. Walking through the pointer movements makes the algorithm click.
Example 1: nums = [3,4,5,1,2]. Start with left=0, right=4. Mid=2, nums[2]=5. Compare 5 > nums[4]=2, so minimum is in the right half. Set left=3. Now left=3, right=4. Mid=3, nums[3]=1. Compare 1 <= nums[4]=2, so set right=3. Now left=right=3. Return nums[3]=1. Correct.
Example 2: nums = [4,5,6,7,0,1,2]. Start with left=0, right=6. Mid=3, nums[3]=7. Compare 7 > nums[6]=2, so set left=4. Now left=4, right=6. Mid=5, nums[5]=1. Compare 1 <= nums[6]=2, so set right=5. Now left=4, right=5. Mid=4, nums[4]=0. Compare 0 <= nums[5]=1, so set right=4. Now left=right=4. Return nums[4]=0. Correct.
Notice how each iteration eliminates roughly half the search space. In the first example we needed 2 iterations for a 5-element array. In the second, 3 iterations for a 7-element array. This is O(log n) in practice.
- 1Initialize left = 0 and right = length - 1
- 2While left < right, compute mid = left + Math.floor((right - left) / 2)
- 3If nums[mid] > nums[right], the pivot is in the right half: set left = mid + 1
- 4Otherwise, the pivot is in the left half (including mid): set right = mid
- 5When left == right, return nums[left] as the minimum
Edge Cases and the Duplicates Variant
Several edge cases are worth considering when solving the find minimum rotated sorted array leetcode problem. Handling them correctly demonstrates thoroughness that interviewers reward.
When the array is not rotated at all — for example [1,2,3,4,5] — the algorithm still works. On the first iteration, nums[mid] <= nums[right], so you keep shrinking right. Eventually left lands on index 0, which is the minimum. No special case is needed.
A two-element array like [2,1] works naturally. Mid=0, nums[0]=2 > nums[1]=1, so left moves to 1. Left equals right, return nums[1]=1. A single-element array is even simpler: left already equals right, so you return immediately.
The harder variant is Find Minimum in Rotated Sorted Array II (LeetCode #154), which allows duplicate elements. When nums[mid] equals nums[right], you cannot determine which half contains the minimum. The solution is to shrink right by 1 — this safely eliminates one element but means the worst case degrades from O(log n) to O(n). For example, [2,2,2,0,2] requires this fallback because mid and right are identical.
- Not rotated [1,2,3,4,5]: algorithm naturally finds minimum at index 0
- Two elements [2,1]: one comparison moves left, done
- Single element [5]: left == right immediately, return nums[0]
- Duplicates (#154): when nums[mid] == nums[right], decrement right by 1
- Worst case with duplicates: O(n) when all elements are equal except one
Watch Out
Find Minimum in Rotated Sorted Array II (#154) adds duplicates — when nums[mid] == nums[right], you can't determine the half, so shrink right by 1. This degrades worst case to O(n).
What Find Minimum in Rotated Sorted Array Teaches
Find Minimum in Rotated Sorted Array is more than just a single LeetCode problem — it is the gateway to an entire family of binary search on rotated arrays questions. The compare-mid-to-right pattern you learn here applies directly to Search in Rotated Sorted Array (#33), where you add a target value search on top of the same rotation logic.
The deeper lesson is about adapting binary search to partially sorted data. Standard binary search requires total order. Rotated array problems teach you to identify which portion is sorted and use that information to eliminate half the search space. This skill extends to problems like finding a peak element, searching in a matrix, and binary search on answer space problems.
Practice this pattern until the mid-to-right comparison is second nature. YeetCode flashcards can help you drill the key decision point — when to move left versus right — through spaced repetition. Once Find Minimum (#153) is locked in, tackle Search in Rotated Sorted Array (#33) to complete the rotated array pattern set.