Find Minimum in Rotated Sorted Array

Mediumbinary-searcharray
Category: Binary Search
Companies that ask this question:
AmazonMicrosoftFacebookGoogle

Approach

Find Minimum in Rotated Sorted Array

Problem Statement

Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:

  • [4,5,6,7,0,1,2] if it was rotated 4 times
  • [0,1,2,4,5,6,7] if it was rotated 7 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

Example 1

Input: nums = [3,4,5,1,2]
Output: 1
Explanation: The original array was [1,2,3,4,5] rotated 3 times.

Example 2

Input: nums = [4,5,6,7,0,1,2]
Output: 0
Explanation: The original array was [0,1,2,4,5,6,7] rotated 4 times.

Example 3

Input: nums = [11,13,15,17]
Output: 11
Explanation: The original array was [11,13,15,17] and it was rotated 4 times (no rotation effect).

Intuition

In a rotated sorted array:

  • One half is always sorted
  • The minimum is at the rotation point (where sorted order breaks)
  • If nums[mid] > nums[right]: minimum is in right half (rotation point is right)
  • If nums[mid] ≤ nums[right]: minimum is in left half or at mid

Key insight: Compare mid with right endpoint to determine which half contains the minimum.

Pattern Recognition

This is Modified Binary Search with these characteristics:

  • Sorted array with rotation
  • Find specific property (minimum value)
  • O(log n) requirement
  • Decision based on comparing mid with boundaries

Approach

  1. Initialize: left = 0, right = n - 1

  2. Binary Search:

    • If nums[left] < nums[right]: Array not rotated, return nums[left]
    • Calculate mid = left + (right - left) / 2
    • If nums[mid] > nums[right]:
      • Rotation point is in right half
      • left = mid + 1
    • Else:
      • Minimum could be at mid or in left half
      • right = mid
  3. Return: nums[left] (minimum element)

Edge Cases

  • Array not rotated (already sorted)
  • Single element
  • Two elements
  • Rotated by n times (back to original)

Complexity Analysis

  • Time Complexity: O(log n)
    • Binary search, eliminating half each iteration
  • Space Complexity: O(1)
    • Only using pointers

Solution

java
1class Solution {
2    public int findMin(int[] nums) {
3        int left = 0;
4        int right = nums.length - 1;
5
6        while (left < right) {
7            // If this portion is sorted, leftmost is minimum
8            if (nums[left] < nums[right]) {
9                return nums[left];
10            }
11
12            int mid = left + (right - left) / 2;
13
14            if (nums[mid] > nums[right]) {
15                // Minimum is in right half
16                left = mid + 1;
17            } else {
18                // Minimum is at mid or in left half
19                right = mid;
20            }
21        }
22
23        return nums[left];
24    }
25}
26
Loading visualizer...