LeetCode ยท #153

Find Minimum in Rotated Sorted Array Solution

Given a rotated sorted array of unique elements, find the minimum element in O(log n).

01
Section One ยท Problem

Problem Statement

๐Ÿท๏ธ

Difficulty

Medium

๐Ÿ”—

LeetCode

Problem #153

๐Ÿ—๏ธ

Pattern

Binary Search โ€” find pivot

A sorted array of unique elements was rotated between 1 and n times. Find the minimum element. The original array was sorted in ascending order.

Example
Input: nums = [3,4,5,1,2] Output: 1 // Originally [1,2,3,4,5], rotated 3 times
Constraints
โ€ข n == nums.length โ€ข 1 โ‰ค n โ‰ค 5000 โ€ข All elements unique โ€ข nums was sorted ascending then rotated
02
Section Two ยท Approach 1

Linear Scan โ€” O(n)

Scan the array and track the minimum. Or find the first element smaller than its predecessor โ€” that's the rotation point.

Problem: O(n). Binary search on the rotation structure gives O(log n) โ€” compare mid to the right boundary to decide which half contains the minimum.
MetricValue
TimeO(n)
SpaceO(1)
03
Section Three ยท Approach 2

Binary Search โ€” O(log n)

Compare nums[mid] to nums[right]. If nums[mid] > nums[right], the minimum is in the right half (the rotation break is there). Otherwise, it's in the left half (including mid).

๐Ÿ’ก Mental model: Imagine a hill with a cliff โ€” the array ascends, then drops to the minimum. If you're standing on the left slope (mid > right), the cliff is to your right. If you're on the right slope (mid โ‰ค right), the cliff is to your left or you're already past it. Walk toward the cliff.
Algorithm
  • Step 1: left = 0, right = n - 1.
  • Step 2: While left < right:
  • Step 3: If nums[mid] > nums[right] โ†’ minimum is in [mid+1, right] โ†’ left = mid + 1.
  • Step 4: Else โ†’ minimum is in [left, mid] โ†’ right = mid.
  • Step 5: Return nums[left].
๐ŸŽฏ Why compare to right, not left?:
  • Comparing to left is ambiguous: nums[left] โ‰ค nums[mid] doesn't tell you if the minimum is left or right (could be an unrotated array).
  • Comparing to right is definitive: if nums[mid] > nums[right], there must be a drop between mid and right.
04
Section Four ยท Trace

Visual Walkthrough

Trace through nums = [3, 4, 5, 1, 2]:

Binary Search โ€” find the rotation point (minimum)
nums = [3, 4, 5, 1, 2] STEP 1: L=0, R=4, mid=2 3 4 5 1 2 L mid R nums[2]=5 > nums[4]=2 โ†’ min is right. L=3 STEP 2: L=3, R=4, mid=3 1 2 L=mid R nums[3]=1 โ‰ค nums[4]=2 โ†’ min is left. R=3 STEP 3: L=R=3 โ†’ nums[3] = 1 โ˜… FOUND! 1 L=R 2 steps to find the rotation point. O(log 5) โ‰ˆ 2.3. Answer: 1 โœ“
05
Section Five ยท Implementation

Code โ€” Java & Python

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

Complexity Analysis

ApproachTimeSpaceTrade-off
Linear ScanO(n)O(1)Simple but slow
Binary Search โ† optimal O(log n) O(1) Compare mid to right boundary
07
Section Seven ยท Edge Cases

Edge Cases & Pitfalls

CaseInputWhy It Matters
Not rotated[1, 2, 3, 4]nums[mid] always โ‰ค nums[right] โ†’ right converges to 0. Min = nums[0].
Single element[1]L==R immediately โ†’ return nums[0].
Two elements[2, 1]mid=0, nums[0]=2 > nums[1]=1 โ†’ L=1. Return nums[1]=1.
Fully rotated[2,3,4,5,1]Min at last position. Binary search converges there.
โš  Common Mistake: Using left โ‰ค right instead of left < right. This is a "find boundary" search, not "find exact target." With โ‰ค, the loop runs one extra time and can cause infinite loops when right = mid and left == right.

โ† Back to Binary Search problems