Find Minimum in Rotated Sorted Array - Leetcode Solution


Step-by-Step Thought Process

Brute Force

  1. Understand the problem: Find the minimum element in a sorted array that has been rotated an unknown number of times.
  2. Initialize minn to infinity to track the smallest number.
  3. Iterate through each number in the array.
  4. If the current number is less than minn, update minn to the current number.
  5. Return minn after the loop.

Code Solution (Brute Force)


                

                

                

                

Why the Brute-Force Solution is Inefficient

The brute-force solution checks every element in the array, ignoring the sorted and rotated properties, leading to:

Optimal Solution

The optimal solution uses binary search to find the minimum in O(log n) time complexity:

  1. Initialize left pointer to 0 and right pointer to the array length minus 1.
  2. While left is less than right, compute the middle index as the average of left and right (integer division).
  3. If the middle element is greater than the rightmost element, the minimum lies in the right half, so adjust left to middle + 1.
  4. Otherwise, the minimum lies in the left half including the middle, so adjust right to middle.
  5. Return the element at the left index after the loop.

Code Solution (Optimal)


                

                

                

                

Detailed Explanation

Understanding the Problem: Find Minimum in Rotated Sorted Array

The “Find Minimum in Rotated Sorted Array” problem involves identifying the smallest element in an array that was initially sorted in ascending order but then rotated at some pivot unknown to you beforehand. The array contains no duplicate elements and is guaranteed to have been rotated at least once.

Example:

  • Input: nums = [4,5,6,7,0,1,2] → Output: 0
  • Input: nums = [3,4,5,1,2] → Output: 1

Why This Problem Matters

This problem is a classic application of binary search under modified conditions. It teaches you to recognize how the sorted property can still be leveraged in rotated arrays to achieve logarithmic time complexity. Such problems are highly relevant in technical interviews and real-world scenarios involving circular buffers, search optimization, and time series analysis.

Optimal Approach: Binary Search in a Rotated Array

The goal is to find the index of the smallest element using binary search in O(log n) time. Since the array is sorted and rotated, the minimum element is the only "pivot point" where the order breaks. Comparing the middle element with the rightmost element helps decide which side the minimum lies on.

Steps:

  1. Initialize two pointers: left = 0 and right = nums.length - 1.
  2. While left < right:
    • Compute mid = Math.floor((left + right) / 2).
    • If nums[mid] > nums[right], the minimum must be in the right half → set left = mid + 1.
    • Otherwise, the minimum is in the left half (including mid) → set right = mid.
  3. Once the loop exits, left will point to the minimum element.
  4. Return nums[left].

Example Walkthrough

Input: nums = [4,5,6,7,0,1,2]

  • left = 0, right = 6 → mid = 3 → nums[3] = 7, nums[6] = 2 → 7 > 2 → left = 4
  • left = 4, right = 6 → mid = 5 → nums[5] = 1, nums[6] = 2 → 1 < 2 → right = 5
  • left = 4, right = 5 → mid = 4 → nums[4] = 0, nums[5] = 1 → 0 < 1 → right = 4
  • left = right = 4 → return nums[4] = 0

Time and Space Complexity

Time Complexity: O(log n), where n is the number of elements in the array.
Space Complexity: O(1), since only constant space is used.

Edge Cases to Consider

  • Array is already sorted (not rotated) → minimum is at index 0
  • Minimum is at the last index → properly handles the right half
  • Array has only one element → return that element

Conclusion

The “Find Minimum in Rotated Sorted Array” problem is a smart application of binary search that demonstrates how to adapt core techniques to new scenarios. Recognizing patterns and utilizing comparisons between midpoints and boundaries allows for an efficient and elegant solution.

Get Personalized Support at AlgoMap Bootcamp 💡