Find Minimum in Rotated Sorted Array — Problem & Solution
Find Minimum in Rotated Sorted Array solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. A medium problem.
Find Minimum in Rotated Sorted Array is a medium coding problem on arrays and binary search, asked in Ola interviews.
- Difficulty: Medium
- Topics: Arrays, Binary Search
- Asked at: Ola
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
A sorted ascending array of unique elements was rotated between 1 and n times. Given the rotated array nums, return its minimum element in O(log n) time.
Example 1
Input: nums = [3,4,5,1,2]
Output: 1
Explanation: [1,2,3,4,5] rotated 3 times.
Example 2
Input: nums = [4,5,6,7,0,1,2]
Output: 0
Example 3
Input: nums = [11,13,15,17]
Output: 11
Explanation: Rotated 4 times — back to sorted.
Constraints
1 <= nums.length <= 30-100 <= nums[i] <= 100Unique values.
Hints for Find Minimum in Rotated Sorted Array
Hint 1
Compare nums[mid] with nums[hi]: if greater, the minimum is to the right of mid.
Hint 2
Shrink toward the unsorted side until lo == hi.
How to solve Find Minimum in Rotated Sorted Array
The minimum is the single point where the array "drops" — the start of the second sorted run. Comparing nums[mid] against the last element of the current range tells you which side of that drop you are on, so you can halve the range without ever seeing the whole array.
Approach
- Keep a range
[lo, hi]and loop whilelo < hi. - Take
mid = lo + (hi - lo) / 2. - If
nums[mid] > nums[hi], the drop is strictly aftermid— setlo = mid + 1. - Otherwise
midcould itself be the minimum — sethi = mid. - When the range collapses,
nums[lo]is the minimum.
Why it works
Comparing against nums[hi] rather than nums[lo] is what makes this clean. If nums[mid] > nums[hi] then mid sits in the first (higher) run while hi sits in the second, so the wrap-around point lies to the right and mid cannot be the answer. Otherwise mid is already in the run containing the minimum, and the minimum is at mid or before it — so keeping mid in range is required. The invariant "the minimum is in [lo, hi]" holds throughout, and the range strictly shrinks, so lo lands on it. A non-rotated array simply always takes the second branch.
Complexity
- Time —
O(log n) - Space —
O(1)
Pitfalls
- Compare with
nums[hi], notnums[lo]. Theloversion needs extra cases to handle an unrotated array. - Use
hi = mid, notmid - 1—midmay be the minimum itself. - The loop condition is
lo < hi; with<=andhi = midit never terminates. - Return the value at
lo, not the index.
Reference solution
Python
from typing import List
def findMin(nums: List[int]) -> int:
lo, hi = 0, len(nums) - 1
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] > nums[hi]:
lo = mid + 1
else:
hi = mid
return nums[lo]Java
public static int findMin(int[] nums) {
int lo = 0, hi = nums.length - 1;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (nums[mid] > nums[hi]) lo = mid + 1;
else hi = mid;
}
return nums[lo];
}C++
int findMin(vector<int>& nums) {
int lo = 0, hi = (int) nums.size() - 1;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (nums[mid] > nums[hi]) lo = mid + 1;
else hi = mid;
}
return nums[lo];
}JavaScript
var findMin = function(nums) {
let lo = 0, hi = nums.length - 1;
while (lo < hi) {
const mid = (lo + hi) >> 1;
if (nums[mid] > nums[hi]) lo = mid + 1;
else hi = mid;
}
return nums[lo];
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays · Binary Search