Home
DSA Patterns
Binary Search
Find Minimum in Rotated Sorted Array
Find Minimum in Rotated Sorted Array (LeetCode 153)
Find Minimum in Rotated Sorted Array (LeetCode 153): find the smallest element of a rotated sorted array of distinct values in O(log n) with binary search.
Problem Understanding
Find Minimum in Rotated Sorted Array: an ascending array of distinct values was rotated between 1 and n times, so [0, 1, 2, 4, 5, 6, 7] might become [4, 5, 6, 7, 0, 1, 2]. Return the minimum element in O(log n) time.
Example: nums = [3, 4, 5, 1, 2] returns 1.
Attempt 1: Scan Every Element
Walk the array and keep the smallest value: O(n) time, O(1) space. It is correct, but it ignores what the problem gives you: the array is sorted except for one drop, and the minimum sits right after that drop. The problem asks for O(log n), which means halving the range at each step.
The Intuition: Find the Side of the Drop
A rotated sorted array is two ascending runs, and the minimum is the first element of the second run. Look at mid and right:
- If
nums[mid] > nums[right], the values fall somewhere betweenmidandright, so the drop, and the minimum, is to the right ofmid:left = mid + 1. - Otherwise
nums[mid..right]is ascending, so nothing to the right ofmidis smaller thannums[mid]: the minimum is atmidor before it,right = mid.
Each step keeps the minimum inside [left, right] and halves the range. When left == right, that index is the minimum.
Find Minimum in Rotated Sorted Array Visualization
READY
L
0
1
2
3
R
4
Left
0
Mid
:
Right
4
Minimum
:
Find the minimum: left = 0, right = 4
Next
mid = 2: compare nums[2] = 5 with nums[4] = 2
Array
L, M and R mark left, mid and right on every step, and everything outside the range is struck out. Watch how comparing nums[mid] with nums[right] always throws away the half that cannot hold the minimum.
Find Minimum in Rotated Sorted Array Dry Run
Input: nums = [4, 5, 6, 7, 0, 1, 2]
left |
right |
mid |
nums[mid] vs nums[right] |
Move |
|---|---|---|---|---|
| 0 | 6 | 3 | 7 > 2 | left = 4 |
| 4 | 6 | 5 | 1 ≤ 2 | right = 5 |
| 4 | 5 | 4 | 0 ≤ 1 | right = 4 |
| 4 | 4 | - | loop ends | return nums[4] = 0 |
Find Minimum in Rotated Sorted Array LeetCode Solution
Binary search on the side of the drop, comparing mid with right.
Find Minimum in Rotated Sorted Array Code
1var findMin = function(nums) {2 let left = 0, right = nums.length - 1;3 while (left < right) {4 const mid = Math.floor((left + right) / 2);5 if (nums[mid] > nums[right]) {6 left = mid + 1;7 } else {8 right = mid;9 }10 }11 return nums[left];12};
Find Minimum in Rotated Sorted Array in Python, Java, C++, TypeScript and C
Find Minimum in Rotated Sorted Array in Python
class Solution:
def findMin(self, nums: List[int]) -> int:
left, right = 0, len(nums) - 1
while left < right:
mid = (left + right) // 2
if nums[mid] > nums[right]:
left = mid + 1
else:
right = mid
return nums[left]Find Minimum in Rotated Sorted Array in Java
class Solution {
public int findMin(int[] nums) {
int left = 0, right = nums.length - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
right = mid;
}
}
return nums[left];
}
}Find Minimum in Rotated Sorted Array in C++
class Solution {
public:
int findMin(vector<int>& nums) {
int left = 0, right = nums.size() - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
right = mid;
}
}
return nums[left];
}
};Find Minimum in Rotated Sorted Array in TypeScript
function findMin(nums: number[]): number {
let left = 0, right = nums.length - 1;
while (left < right) {
const mid = Math.floor((left + right) / 2);
if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
right = mid;
}
}
return nums[left];
}Find Minimum in Rotated Sorted Array in C
int findMin(int* nums, int numsSize) {
int left = 0, right = numsSize - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
right = mid;
}
}
return nums[left];
}Edge Cases & Common Mistakes
- Not rotated (
[11, 13, 15, 17]):nums[mid]is never larger thannums[right], sorightwalks down to0. - One element: the loop never runs and
nums[0]is returned. while (left <= right)withright = mid: whenleft == rightthe loop never ends. Useleft < right.right = mid - 1: skipsmidwhen it is the minimum, as in[3, 1, 2].- Duplicates: this version assumes distinct values; see LeetCode 154 for the duplicate case.
Summary
Binary search does not need a target, only a test that tells you which half to keep. Here the test is nums[mid] > nums[right]: it points at the side of the drop, and the minimum is the first element after it. The same idea drives Search in Rotated Sorted Array.
More practice: DSA interview questions with answers · patterns cheat sheet
