Find Minimum in Rotated Sorted Array
Find Minimum in Rotated Sorted Array
Section titled “Find Minimum in Rotated Sorted Array”
Medium
Day 2 • Striver Blind 75
📌 Problem Overview
Section titled “📌 Problem Overview”Suppose an array of length n sorted in ascending order is rotated between 1 and n times. Given the sorted rotated array nums of unique elements, return the minimum element of this array.
You must write an algorithm that runs in O(log n) time.
Examples & Constraints
Section titled “Examples & Constraints”Example 1:
- Input:
nums = [3,4,5,1,2] - Output:
1
Example 2:
- Input:
nums = [4,5,6,7,0,1,2] - Output:
0
Constraints:
n == nums.length1 ≤ n ≤ 5000-5000 ≤ nums[i] ≤ 5000All integers in nums are unique.
💡 Approach & Intuition
Section titled “💡 Approach & Intuition”In a rotated sorted array, the elements to the left of the pivot point are greater than elements to the right of the pivot point. We can use binary search to locate this inflection point.
🎯 Pattern Recognition
Section titled “🎯 Pattern Recognition”Pattern: Inflection Point Binary Search
Compare nums[mid] with nums[right]. If nums[mid] > nums[right], the min is in the right half; else, it is in the left half (including mid).
📊 Step-by-Step Execution (Mermaid Diagram)
Section titled “📊 Step-by-Step Execution (Mermaid Diagram)”graph TD Start["Input Data"] --> Process["Process Element by Element"] Process --> Lookup{"Hash Map / Set Lookup"} Lookup -- "Match Found" --> Return["Return Indices / Result"] Lookup -- "No Match" --> Store["Store in Map / Set"] Store --> Process🐢 Brute Force Solution
Section titled “🐢 Brute Force Solution”function findMin(nums) { let min = nums[0]; for (let i = 1; i < nums.length; i++) { if (nums[i] < min) min = nums[i]; } return min;}- Time Complexity:
O(n) - Space Complexity:
O(1) - Explanation: Linear search of the minimum.
⚡ Optimized Solution
Section titled “⚡ Optimized Solution”function findMin(nums) { let left = 0; let 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];}- Time Complexity:
O(log n) - Space Complexity:
O(1) - Explanation: Binary search comparing mid and right.
🐾 Step-by-Step Walkthrough
Section titled “🐾 Step-by-Step Walkthrough”- Initialize State: Setup necessary pointers, dynamic programming arrays, or hash maps.
- Iterate & Evaluate: Process the input according to the boundary conditions.
- Update & Return: Compute the optimal answer and return early or at termination.
🎙️ FAANG Interview Pitch
Section titled “🎙️ FAANG Interview Pitch”- Compares middle with right boundary to identify sorted direction
- Cuts search space in half based on inflection pivot
- Runs in logarithmic O(log n) time
💡 Progressive Hints
Section titled “💡 Progressive Hints”- Compare
nums[mid]withnums[right]. - If
nums[mid] > nums[right], the minimum must be to the right ofmid. - Otherwise, the minimum is at
midor to its left.