15% off all plans & roadmaps Use code
See plansFind Minimum in Rotated Sorted Array
- LeetCode 153
- Medium
- Binary Search Patterns
- Find Min/Max in Rotated Sorted Array
Updated
TL;DR: A sorted array of unique numbers was rotated, and you must return its smallest value in O(log n). Binary search works if you compare the middle element with the right end to decide which half holds the drop.
Problem
A sorted array of length n in ascending order has been rotated between 1 and n times. For example, nums = [0,1,2,4,5,6,7] can become:
[4,5,6,7,0,1,2]after 4 rotations.[0,1,2,4,5,6,7]after 7 rotations.
One rotation turns [a[0], a[1], a[2], ..., a[n-1]] into [a[n-1], a[0], a[1], a[2], ..., a[n-2]].
You are given such a rotated array nums whose elements are all unique. Return its minimum element. Your algorithm must run in O(log n) time.
Example 1
Input: nums = [3,4,5,1,2] Output: 1 Explanation: The original array was [1,2,3,4,5], rotated 3 times.
Example 2
Input: nums = [4,5,6,7,0,1,2] Output: 0 Explanation: The original array was [0,1,2,4,5,6,7], rotated 4 times.
Example 3
Input: nums = [11,13,15,17] Output: 11 Explanation: The original array was [11,13,15,17], rotated 4 times.
Constraints
n == nums.length1 <= n <= 5000-5000 <= nums[i] <= 5000- All integers of
numsare unique. numsis sorted and rotated between1andntimes.
Constraints that matter
- The statement demands O(log n) time, so a full scan is rejected by the rules even though n is only 5000. The interviewer wants binary search.
- All values are unique, so
nums[mid]never equalsnums[right]whenmid < right. You need no tie-breaking branch. - The array is rotated between 1 and n times, and n rotations leave it fully sorted. Your search must handle the unrotated case, where the minimum sits at index 0.
- n can be 1, so the loop must tolerate a single element without reading outside the array.
- n up to 5000 means an O(n log n) sort would pass the tests, but it still breaks the stated time limit. Values fit in a 32-bit int, so overflow is not a concern for the values, only a habit for the midpoint.
Hints
Hint 1
A rotated sorted array is two ascending blocks glued together.
Hint 2
The most direct idea ignores the structure.
Solution
Intuition
A rotated sorted array is two ascending blocks glued together. The left block holds the larger values and the right block holds the smaller ones. The minimum is the first element of the right block, and it is the only element smaller than its left neighbour.
Picture a sorted row of books that someone slid so the last few books now sit at the front. The right end of the range is the fixed reference book, and each probe checks whether the middle book sits above or below it, which tells you which side the break is on.
A normal binary search needs a target to compare against. Here you have none, so you compare nums[mid] with the last element of the current range, nums[right]. The right end always belongs to the smaller block, or the range is fully sorted. That makes it a stable reference point.
If nums[mid] > nums[right], then mid sits in the big left block and the drop lies after mid. The minimum is strictly to the right. If nums[mid] < nums[right], the stretch from mid to right is ascending, so nothing after mid can be smaller. The minimum is mid or something to its left.
Each comparison throws away about half the range, which gives O(log n). The problem gives you no target value. Whenever you must find a boundary or extreme in a structure with one sharp break, ask which half the break lives in and compare against a fixed end.
Worked example
Input: nums = [2, 3, 4, 5, 6, 7, 8, 9, 1] Output: 1 Explanation: The original sorted array was [1, 2, 3, 4, 5, 6, 7, 8, 9]. One rotation moved the 1 from the back to the front of the array... and then the array was rotated so that 1 ended at the last index. The values 2 through 9 form the ascending left block, and the lone 1 forms the right block, so the minimum is 1.
Approach 1: Sort, then take the first element
The most direct idea ignores the structure. The minimum of any collection is the first element once it is sorted, so sort a copy of the array and read index 0.
This works for any input, rotated or not. It throws away the fact that the array is already almost sorted, and it pays O(n log n) to rebuild an order you mostly had.
Steps
- Copy
numsso the caller's array is not changed. - Sort the copy in ascending order.
- Return the element at index 0 of the sorted copy.
Dry run
Start with [2, 3, 4, 5, 6, 7, 8, 9, 1].
- Copy the array and sort it. The copy becomes
[1, 2, 3, 4, 5, 6, 7, 8, 9]. - Read index 0. The value is
1. - Return
1, which matches the expected output.
Complexity
| Time | O(n log n) |
|---|---|
| Space | O(n) |
Here n is the length of nums. A comparison sort does O(n log n) comparisons on n elements. The copy holds n values, so the extra space is O(n). Some library sorts need extra working memory too, but the copy already dominates.
PythonTested in our runner
from typing import List, Optional
class Solution:
def findMin(self, nums: List[int]) -> int:
# sorted() builds a new list, so nums is untouched
return sorted(nums)[0]
JavaTested in our runner
import java.util.*;
class Solution {
public int findMin(int[] nums) {
int[] copy = Arrays.copyOf(nums, nums.length);
Arrays.sort(copy);
return copy[0];
}
}
C++Tested in our runner
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int findMin(vector<int>& nums) {
vector<int> copy = nums;
sort(copy.begin(), copy.end());
return copy[0];
}
};
JavaScriptTested in our runner
/**
* @param {number[]} nums
* @return {number}
*/
var findMin = function(nums) {
// spread makes a copy; the comparator forces numeric order
const copy = [...nums].sort((a, b) => a - b);
return copy[0];
};
GoTested in our runner
import "sort"
func findMin(nums []int) int {
copyNums := make([]int, len(nums))
copy(copyNums, nums)
sort.Ints(copyNums)
return copyNums[0]
}
C#Tested in our runner
public class Solution {
public int findMin(int[] nums) {
int[] copy = (int[])nums.Clone();
Array.Sort(copy);
return copy[0];
}
}
Approach 2: Linear scan
What's slow: sorting orders every element when you only need one value. Ordering costs O(n log n) and an extra copy.
The insight that fixes it: a minimum needs a single pass. Keep the smallest value seen so far and compare each element against it. This needs no extra memory and drops the log factor. It still reads every element, so it cannot meet the O(log n) requirement.
Steps
- Set
besttonums[0]. - Walk the array from index 1 to the end.
- If the current element is smaller than
best, store it inbest. - Return
bestafter the last element.
Dry run
Start with [2, 3, 4, 5, 6, 7, 8, 9, 1].
best = 2from index 0.- Indices 1 to 7 hold 3, 4, 5, 6, 7, 8, 9. None is below 2, so
beststays 2. - Index 8 holds 1. Since 1 < 2,
best = 1. - The loop ends and the function returns
1.
Complexity
| Time | O(n) |
|---|---|
| Space | O(1) |
Here n is the length of nums. The loop body runs n - 1 times and each iteration does one comparison, so time is O(n). The only extra storage is the variable best, so space is O(1). The scan never exploits the sorted structure, which is why it cannot beat linear time.
PythonTested in our runner
from typing import List, Optional
class Solution:
def findMin(self, nums: List[int]) -> int:
best = nums[0]
for x in nums[1:]:
if x < best:
best = x
return best
JavaTested in our runner
import java.util.*;
class Solution {
public int findMin(int[] nums) {
int best = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] < best) {
best = nums[i];
}
}
return best;
}
}
C++Tested in our runner
#include <vector>
using namespace std;
class Solution {
public:
int findMin(vector<int>& nums) {
int best = nums[0];
for (size_t i = 1; i < nums.size(); i++) {
if (nums[i] < best) {
best = nums[i];
}
}
return best;
}
};
JavaScriptTested in our runner
/**
* @param {number[]} nums
* @return {number}
*/
var findMin = function(nums) {
let best = nums[0];
for (let i = 1; i < nums.length; i++) {
if (nums[i] < best) {
best = nums[i];
}
}
return best;
};
GoTested in our runner
func findMin(nums []int) int {
best := nums[0]
for i := 1; i < len(nums); i++ {
if nums[i] < best {
best = nums[i]
}
}
return best
}
C#Tested in our runner
public class Solution {
public int findMin(int[] nums) {
int best = nums[0];
for (int i = 1; i < nums.Length; i++) {
if (nums[i] < best) {
best = nums[i];
}
}
return best;
}
}

Solution pattern
Find Min/Max in Rotated Sorted Array
Part of Binary Search Patterns
Approach 3: Binary search against the right endOptimalFind Min/Max in Rotated Sorted Array
What's slow: the linear scan inspects every element, including the ones the sorted structure already rules out. After one comparison in the middle you can tell which half cannot hold the minimum.
The insight that fixes it: compare nums[mid] with nums[right]. If nums[mid] > nums[right], the drop from large to small happens after mid, so move left = mid + 1. Otherwise the stretch mid..right is ascending, so the minimum is mid or to its left, and you set right = mid. Keep mid in the range on that branch, since it may be the answer.
Loop while left < right. When the two pointers meet, one candidate remains and it is the minimum.
Steps
- Set
left = 0andright = len(nums) - 1. - While
left < right, computemid = left + (right - left) / 2with integer division. - If
nums[mid] > nums[right], the minimum is right ofmid, so setleft = mid + 1. - Otherwise the minimum is at
midor left of it, so setright = mid. - When the loop ends,
left == right. Returnnums[left].
Dry run
Trace on [2, 3, 4, 5, 6, 7, 8, 9, 1], where n = 9 and the final answer is 1.
| Step | left | right | mid | nums[mid] | nums[right] | Decision |
|---|---|---|---|---|---|---|
| start | 0 | 8 | range is the whole array | |||
| 1 | 0 | 8 | 4 | 6 | 1 | 6 > 1, so left = 5 |
| 2 | 5 | 8 | 6 | 8 | 1 | 8 > 1, so left = 7 |
| 3 | 7 | 8 | 7 | 9 | 1 | 9 > 1, so left = 8 |
| end | 8 | 8 | left == right, return nums[8] = 1 |
The range shrank from 9 elements to 6, then 4, then 2, then 1. Every middle value was larger than the right end of 1, so the search kept moving right toward the drop and landed on the minimum 1.
Complexity
| Time | O(log n) |
|---|---|
| Space | O(1) |
Here n is the length of nums. With a range of size s, the new range has at most ceil(s / 2) elements: left = mid + 1 keeps the upper part, and right = mid keeps the lower part including mid. Halving from n down to 1 takes about log2(n) iterations, which is at most 13 for n = 5000. Each iteration does constant work and the code stores only three integers, so space is O(1).
PythonTested in our runner
from typing import List, Optional
class Solution:
def findMin(self, nums: List[int]) -> int:
left, right = 0, len(nums) - 1
while left < right:
mid = left + (right - left) // 2
if nums[mid] > nums[right]:
# drop is after mid, so the minimum is on the right
left = mid + 1
else:
# mid..right is ascending, mid may be the minimum
right = mid
return nums[left]
JavaTested in our runner
import java.util.*;
class Solution {
public int findMin(int[] nums) {
int left = 0;
int 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];
}
}
C++Tested in our runner
#include <vector>
using namespace std;
class Solution {
public:
int findMin(vector<int>& nums) {
int left = 0;
int right = (int)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];
}
};
JavaScriptTested in our runner
/**
* @param {number[]} nums
* @return {number}
*/
var findMin = function(nums) {
let left = 0;
let right = nums.length - 1;
while (left < right) {
const mid = left + Math.floor((right - left) / 2);
if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
right = mid;
}
}
return nums[left];
};
GoTested in our runner
func findMin(nums []int) int {
left, right := 0, len(nums)-1
for left < right {
mid := left + (right-left)/2
if nums[mid] > nums[right] {
left = mid + 1
} else {
right = mid
}
}
return nums[left]
}
C#Tested in our runner
public class Solution {
public int findMin(int[] nums) {
int left = 0;
int 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];
}
}
Common mistakes
- Writing
while left <= righttogether withright = mid. Whenleft == rightthe body keeps settingright = midto the same value and never exits. Usewhile left < right. - Setting
right = mid - 1in the else branch. The element atmidcan be the minimum, for example in[3, 1, 2]after the first step, so this discards the answer. Useright = mid. - Comparing
nums[mid]withnums[left]to pick a side. On an unrotated array like[1, 2, 3],nums[mid] > nums[left]is true but the minimum is on the left. Compare withnums[right]instead, which works for every rotation. - Writing
left = midin the first branch. Withleft < right,midcan equalleft, so the range stops shrinking and the loop hangs. Useleft = mid + 1, which is safe becausenums[mid]is larger thannums[right]and cannot be the minimum. - Looking for a drop with
nums[i] > nums[i + 1]and returningnums[i + 1]. On an array rotated n times, no drop exists, so the code falls through. The comparison againstnums[right]needs no special case. - Using
nums[0]as a default return when nothing is found, or skipping the single-element case. For[5]the loop never runs andnums[left]must still be returned. Returnnums[left]after the loop in all cases.
Edge cases
[1]: one element, the loop is skipped, and the answer is 1.[1, 2, 3, 4, 5]: rotated n times, so it is fully sorted. Every middle is below the right end,rightkeeps moving tomid, and the answer is 1 at index 0.[2, 1]: two elements.mid = 0,nums[0] = 2 > nums[1] = 1, soleft = 1and the answer is 1.[5, 1, 2, 3, 4]: the minimum is at index 1 right after the first element. The search must not skip it, and returns 1.[2, 3, 4, 5, 6, 7, 8, 9, 1]: the minimum is the last element. The search moves right every time and ends at index 8.- Negative values such as
[-1, 0, 3, -5]: comparisons work the same on negatives, and the answer is -5.
Follow-ups interviewers ask
- What if the array can contain duplicates?
- Then
nums[mid] == nums[right]becomes possible. You cannot tell which side holds the minimum, so you shrink the range withright -= 1. That is safe because the value you drop equalsnums[mid], which stays in the range. The average case is still logarithmic, but an array of all equal values forces O(n) in the worst case. - How do you find how many times the array was rotated?
- Run the same search and return the index
leftinstead of the value. The minimum's index equals the number of rotations modulo n, where an index of 0 means the array is unrotated. The cost stays O(log n). - How would you search for a target in a rotated sorted array?
- Find the pivot index with this search, then binary search the left block or the right block depending on where the target falls. You can also do it in one pass by checking which half is sorted at each step. Both options are O(log n).
Find Minimum in Rotated Sorted Array FAQ
What is the time complexity of Find Minimum in Rotated Sorted Array?
The optimal solution is O(log n) time and O(1) space, where n is the array length. Each iteration discards about half of the remaining range. A linear scan is O(n) and sorting is O(n log n), and neither meets the required bound.
Why use binary search for Find Minimum in Rotated Sorted Array?
The array is two ascending blocks with one drop, so a single comparison between nums[mid] and nums[right] tells you which half contains the drop. That halves the search range every step. The problem also states the O(log n) requirement, which only binary search can reach here.
Why compare nums[mid] with nums[right] and not nums[left]?
The right end is either in the smaller block or the whole range is sorted, so comparing against it gives a clean decision in all cases. Comparing with nums[left] is ambiguous when the range is already sorted, because the minimum is then at left while nums[mid] > nums[left] suggests moving right.
Can Find Minimum in Rotated Sorted Array be solved in O(1) extra space?
Yes. The binary search keeps only left, right and mid, so extra space is O(1). The sorting approach needs a copy and uses O(n).
Why is it right = mid and not right = mid - 1?
When nums[mid] < nums[right], the element at mid may be the minimum itself. Setting right = mid - 1 could throw it away. Keeping mid in the range is safe, and the loop still ends because mid < right whenever left < right.
Does the same code handle an array that was not rotated?
Yes. A fully sorted array is the case of n rotations. Every nums[mid] is below nums[right], so right slides left to index 0 and the function returns nums[0].
Asked at
See which companies ask this — Pro
Related problems
- Search in Rotated Sorted Array · same technique: Find Min/Max in Rotated Sorted Array
- Search in Rotated Sorted Array II · same technique: Find Min/Max in Rotated Sorted Array
- Find Peak Element · same technique: Find Min/Max in Rotated Sorted Array
- Peak Index in a Mountain Array · same technique: Find Min/Max in Rotated Sorted Array
- Find in Mountain Array · same technique: Find Min/Max in Rotated Sorted Array
- Koko Eating Bananas · same pattern: Binary Search Patterns
- Search a 2D Matrix · same pattern: Binary Search Patterns
- Find K Closest Elements · same pattern: Binary Search Patterns
- Capacity To Ship Packages Within D Days · same pattern: Binary Search Patterns
- Find First and Last Position of Element in Sorted Array · same pattern: Binary Search Patterns