LeetCode ยท #33

Search in Rotated Sorted Array Solution

Search for a target in a sorted array that has been rotated at an unknown pivot. Return its index or -1.

01
Section One ยท Problem

Problem Statement

๐Ÿท๏ธ

Difficulty

Medium

๐Ÿ”—

LeetCode

Problem #33

๐Ÿ—๏ธ

Pattern

Binary Search โ€” rotated array

An array originally sorted in ascending order was rotated at some pivot. Given the rotated array nums (all elements unique) and a target, return the index of target or -1. You must achieve O(log n) runtime.

Example
Input: nums = [4,5,6,7,0,1,2], target = 0 Output: 4 // Originally [0,1,2,4,5,6,7], rotated at index 4
Constraints
โ€ข 1 โ‰ค nums.length โ‰ค 5000 โ€ข -10โด โ‰ค nums[i] โ‰ค 10โด โ€ข All values unique โ€ข Must run in O(log n)
02
Section Two ยท Approach 1

Linear Scan โ€” O(n)

Walk through the array. Correct but violates the O(log n) requirement.

Problem: O(n) ignores the partial sorted structure. In a rotated sorted array, at least one half (left or right of mid) is always sorted โ€” binary search on that half.
MetricValue
TimeO(n)
SpaceO(1)
03
Section Three ยท Approach 2

Modified Binary Search โ€” O(log n)

At each step, determine which half is sorted (compare nums[left] to nums[mid]). If target lies in the sorted half's range, search there. Otherwise, search the other half.

๐Ÿ’ก Mental model: Imagine a clock with unevenly spaced numbers. At any point you look (mid), you can tell if the left half or right half goes in order. If your target number fits within the ordered half, search there. If not, go to the "chaotic" half โ€” which will have its own ordered sub-section when you halve again.
Algorithm
  • Step 1: left = 0, right = n - 1.
  • Step 2: Compute mid. If nums[mid] == target โ†’ return mid.
  • Step 3: If nums[left] โ‰ค nums[mid] (left half sorted):
  • If nums[left] โ‰ค target < nums[mid] โ†’ right = mid - 1. Else โ†’ left = mid + 1.
  • Step 4: Else (right half sorted):
  • If nums[mid] < target โ‰ค nums[right] โ†’ left = mid + 1. Else โ†’ right = mid - 1.
๐ŸŽฏ Key insight:
  • A rotated sorted array always has one sorted half.
  • By checking nums[left] โ‰ค nums[mid], we identify which half is sorted.
  • We can then definitively decide if the target is in the sorted range โ€” if yes, search there; if no, search the other half.
04
Section Four ยท Trace

Visual Walkthrough

Trace through nums = [4, 5, 6, 7, 0, 1, 2], target = 0:

Modified Binary Search โ€” identify sorted half
nums = [4, 5, 6, 7, 0, 1, 2] target = 0 STEP 1: L=0, R=6, mid=3 โ†’ nums[3]=7 โ‰  0 4 5 6 7 0 1 2 L mid R โ† sorted [4,5,6,7] โ€” target 0 not here โ†’ search right: L=4 STEP 2: L=4, R=6, mid=5 โ†’ nums[5]=1 โ‰  0 4 5 6 7 0 1 2 L mid R sorted [0,1] โ€” target 0 IS here โ†’ search left: R=4 STEP 3: L=4, R=4, mid=4 โ†’ nums[4]=0 == 0 โ˜… FOUND! 0 L=R=mid 3 steps โ€” O(log 7) โ‰ˆ 2.8. Binary search on rotated array. Answer: 4 โœ“
05
Section Five ยท Implementation

Code โ€” Java & Python

Java โ€” Modified Binary Search
class Solution { public int search(int[] nums, int target) { int left = 0, right = nums.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (nums[mid] == target) return mid; if (nums[left] <= nums[mid]) { // left half sorted if (nums[left] <= target && target < nums[mid]) right = mid - 1; else left = mid + 1; } else { // right half sorted if (nums[mid] < target && target <= nums[right]) left = mid + 1; else right = mid - 1; } } return -1; } }
Python โ€” Modified Binary Search
class Solution: def search(self, nums: list[int], target: int) -> int: left, right = 0, len(nums) - 1 while left <= right: mid = (left + right) // 2 if nums[mid] == target: return mid if nums[left] <= nums[mid]: # left half sorted if nums[left] <= target < nums[mid]: right = mid - 1 else: left = mid + 1 else: # right half sorted if nums[mid] < target <= nums[right]: left = mid + 1 else: right = mid - 1 return -1
06
Section Six ยท Analysis

Complexity Analysis

ApproachTimeSpaceTrade-off
Linear ScanO(n)O(1)Ignores structure
Modified Binary Search โ† optimal O(log n) O(1) One sorted half always identifiable
07
Section Seven ยท Edge Cases

Edge Cases & Pitfalls

CaseInputWhy It Matters
No rotation[1,2,3,4,5]Standard sorted array. Left half always sorted โ†’ works normally.
Rotated by 1[5,1,2,3,4]Pivot at index 0. Right half sorted for most steps.
Single element[3], target=3mid=0, found immediately.
Target not found[4,5,6,7,0,1,2], target=3Search space exhausted โ†’ return -1.
Two elements[3,1], target=1mid=0, nums[0]=3โ‰คnums[0] left sorted, target not in [3,3] โ†’ go right.
โš  Common Mistake: Using < instead of โ‰ค in nums[left] โ‰ค nums[mid]. When left == mid (two-element subarray), the left "half" is a single element and is sorted. Without โ‰ค, you'd incorrectly assume the right half is sorted and make wrong decisions.

โ† Back to Binary Search problems