Advertisement

Find Minimum in Rotated Sorted Array - LeetCode 153 Solution

Find Minimum in Rotated Sorted Array - Complete Solution Guide

Find Minimum in Rotated Sorted Array is LeetCode problem 153, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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. 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 r

Detailed Explanation

The problem asks us to find the minimum element in a sorted array that has been rotated between 1 and n times. A rotated array is one where some portion of the array has been moved from the beginning to the end, or vice versa. For example, [0, 1, 2, 4, 5, 6, 7] rotated 4 times becomes [4, 5, 6, 7, 0, 1, 2]. The input is a list of unique integers that is sorted and rotated. The output should be the smallest element in the rotated array. The core challenge is to find this minimum element efficiently, specifically in O(log n) time complexity.

Solution Approach

The provided solution utilizes a modified binary search algorithm. The algorithm maintains two pointers, 'left' and 'right', that initially point to the start and end of the array, respectively. The core idea is to repeatedly divide the search space in half based on the relationship between the middle element and the rightmost element. This process continues until 'left' and 'right' converge at the index of the minimum element.

Step-by-Step Algorithm

  1. Step 1: Initialize 'left' to 0 and 'right' to the length of the array minus 1.
  2. Step 2: While 'left' is less than 'right', calculate the middle index 'mid' as left + (right - left) // 2 (or (left + right) / 2 to prevent overflow).
  3. Step 3: Compare nums[mid] with nums[right].
  4. Step 4: If nums[mid] > nums[right], the minimum element lies in the right half of the array, so update 'left' to mid + 1.
  5. Step 5: Otherwise (if nums[mid] <= nums[right]), the minimum element lies in the left half (including mid), so update 'right' to mid.
  6. Step 6: Repeat steps 2-5 until 'left' and 'right' are equal.
  7. Step 7: Return nums[left] (or nums[right], as they are equal at this point), which is the minimum element.

Key Insights

  • Insight 1: The key observation is that the rotated array is still *partially* sorted. This allows us to use binary search to narrow down the search space.
  • Insight 2: The comparison between the middle element and the rightmost element helps determine which half of the array contains the minimum value. If nums[mid] > nums[right], the minimum element is in the right half; otherwise, it's in the left half (including mid).
  • Insight 3: The algorithm leverages the 'sorted' aspect of the array by checking if array is rotated. If not rotated, first element is the minimum.

Complexity Analysis

Time Complexity: O(log n)

Space Complexity: O(1)

Topics

This problem involves: Array, Binary Search.

Companies

Asked at: Cisco, FreshWorks, Goldman Sachs, IBM, Nutanix, PayPal, Paytm, ServiceNow, Synopsys, Tesla, TikTok, Yahoo, Yandex, eBay.