Find Minimum in Rotated Sorted Array

Quick Summary: To find the minimum element in a rotated sorted array efficiently, use a binary search algorithm. By comparing the middle element to the rightmost element, you can eliminate half of the search space in each step, achieving an optimal O(log n) time complexity. This approach guarantees finding the smallest value even when the sorted array is rotated.

https://leetcode.com/problems/find-minimum-in-rotated-sorted-array

Problem Statement: Find Minimum in Rotated Sorted Array

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.

Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].

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.

Algorithmic Approach and Key Observations

  1. Understanding Rotated Sorted Arrays: A rotated sorted array is simply a sorted array that has been shifted at a specific pivot point. For instance, the original array [1, 2, 3, 4, 5] can become [4, 5, 1, 2, 3] or [3, 4, 5, 1, 2].
  2. Identifying the Minimum Element: The minimum element represents the exact point of rotation. It is the only element in the entire rotated array that is strictly smaller than its preceding element. If there is no rotation (e.g., [1, 2, 3, 4, 5]), the very first element remains the smallest.
  3. Applying the Binary Search Approach: Because the given array is initially sorted before the rotation, we can leverage a binary search strategy to locate the minimum element in exactly O(log n) time, avoiding a slower linear scan.

Steps to Solve the Rotated Array Problem

  1. Initialize Binary Search Pointers: Set up two pointers: low = 0 to mark the start of the array and high = n-1 to mark the end of the array.
  2. Execute the Search Loop: While low < high, calculate the middle index using the formula mid = low + (high - low) / 2. This prevents potential integer overflow.
  3. Evaluate the Middle Element: Depending on the relationship between arr[mid] and arr[high]:
    • If arr[mid] > arr[high]: The minimum value must reside in the right half of the array. Thus, update the lower bound by setting low = mid + 1.
    • Otherwise, the minimum value must be located in the left half, or it could potentially be the mid element itself. Thus, update the upper bound by setting high = mid.
  4. Return the Discovered Minimum Result: When the loop terminates (low == high), the pointer low will correctly indicate the index of the minimum element. Output the element located at arr[low].

Algorithm Complexity Analysis

  • Time Complexity: The binary search algorithm operates in an optimal O(log n) time. This is achieved by systematically halving the viable search space during every single iteration.
  • Space Complexity: This solution requires only O(1) auxiliary space. We exclusively manipulate integer pointer indices and access the underlying array structure directly in-place without allocating additional memory arrays.

Optimized Implementation in C++

Below is the highly efficient C++ implementation solving the "Find Minimum in Rotated Sorted Array" problem:

#include <iostream>
#include <vector>
using namespace std;

int findMinimum(const vector<int>& arr) {
    int low = 0, high = arr.size() - 1;

    // Binary search to find the minimum element in O(log n) time
    while (low < high) {
        int mid = low + (high - low) / 2;

        // If the middle element is strictly greater than the last element,
        // the rotation pivot (minimum) must reside in the right half
        if (arr[mid] > arr[high]) {
            low = mid + 1;
        } 
        // Otherwise, the minimum is located in the left half (or might be at mid)
        else {
            high = mid;
        }
    }

    // At loop termination, low == high and correctly points to the minimum element
    return arr[low];
}

int main() {
    vector<int> arr = {4, 5, 6, 7, 0, 1, 2}; // Example rotated array scenario
    cout << "The minimum element is: " << findMinimum(arr) << endl;

    return 0;
}

Frequently Asked Questions

How does binary search find the minimum in a rotated sorted array?
Binary search finds the minimum in a rotated sorted array by continuously evaluating the middle element against the rightmost element. If the middle is greater, the minimum is to the right. Otherwise, it is to the left. This systematically halves the search space, ensuring an O(log n) time complexity.

What is the time complexity to find the minimum in a rotated sorted array?
The optimal time complexity is O(log n). By using a modified binary search approach, the algorithm eliminates half of the remaining array elements during each comparison step.

Can I solve the rotated sorted array minimum problem in O(n) time?
Yes, you can solve it in O(n) time by simply iterating through the array and finding the smallest value linearly. However, most algorithmic interviews and performance requirements demand the optimized O(log n) binary search solution.