Find Minimum in Rotated Sorted Array
1 approach, code in every language
Problem description
Imagine an array that was originally sorted in ascending order and then rotated to the left some unknown number of times, so a slice of the front got moved to the back. Every element is unique. Your job is to return the smallest value that remains hidden somewhere inside this rotated arrangement.
A sweep across all elements would obviously work, but the array's structure lets you discard half of the remaining candidates at each step, so an efficient solution should run in logarithmic time.
Example 1
Input: nums = [3,4,5,1,2]
Output: 1
The original [1,2,3,4,5] was rotated so the minimum 1 landed in the middle.
Example 2
Input: nums = [4,5,6,7,0,1,2]
Output: 0
After rotation the smallest value 0 sits at index 4.
Constraints
n == nums.length1 ≤ n ≤ 5000-5000 ≤ nums[i] ≤ 5000All integers in nums are unique
Create a free account to see the solution
You get the step-by-step animation, the intuition and clean code in every language. Free, no credit card.
After the pattern
Now solve one you haven't seen.
Wrapped Buoy Low Offset is our own problem on the technique behind Find Minimum in Rotated Sorted Array: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.
1,194 problems · 15,062 graded tests · free