Search in Rotated Sorted Array
Search in Rotated Sorted Array
Section titled “Search in Rotated Sorted Array”
Medium
Day 2 • Striver Blind 75
📌 Problem Overview
Section titled “📌 Problem Overview”Search for a target in a rotated sorted array. The array was sorted then rotated at an unknown pivot. Must be O(log n).
Examples & Constraints
Section titled “Examples & Constraints”Example 1:
- Input:
nums = [4,5,6,7,0,1,2], target = 0 - Output:
4
Example 2:
- Input:
nums = [4,5,6,7,0,1,2], target = 3 - Output:
-1
Constraints:
1 ≤ nums.length ≤ 5000All values are unique.
💡 Approach & Intuition
Section titled “💡 Approach & Intuition”Extends binary search to handle rotated sorted arrays. Tests deep understanding of the binary search invariant.
🎯 Pattern Recognition
Section titled “🎯 Pattern Recognition”Pattern: Modified Binary Search
Even in a rotated array, at least one half is fully sorted. Determine which half, then check if target lies in it.
📊 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 search(nums, target) { return nums.indexOf(target); }- Time Complexity:
O(n) - Space Complexity:
O(1) - Explanation: Linear scan via indexOf.
⚡ Optimized Solution
Section titled “⚡ Optimized Solution”function search(nums, target) { let left = 0, right = nums.length - 1; while (left <= right) { const mid = Math.floor((left + right) / 2); if (nums[mid] === target) return mid; if (nums[left] <= nums[mid]) { if (target >= nums[left] && target < nums[mid]) right = mid - 1; else left = mid + 1; } else { if (target > nums[mid] && target <= nums[right]) left = mid + 1; else right = mid - 1; } } return -1;}- Time Complexity:
O(log n) - Space Complexity:
O(1) - Explanation: Modified binary search identifying sorted halves.
🐾 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”- Check which half is sorted via nums[left] <= nums[mid]
- If target is in sorted half, search there
- Otherwise search the other half
💡 Progressive Hints
Section titled “💡 Progressive Hints”- One half is always sorted.
- Compare nums[left] with nums[mid] to determine which half.
- Check if target falls in the sorted half.