KatInterview

153. Find Minimum in Rotated Sorted Array

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to find the minimum element in a sorted array that has been rotated. The array contains unique elements. A key constraint is that the algorithm must run in O(log n) time, which strongly suggests using binary search.

Solution

Find Minimum in Rotated Sorted Array in Python

from typing import List

class Solution:
    def findMin(self, nums: List[int]) -> int:
        left, right = 0, len(nums) - 1
        
        # The loop continues as long as the search space has more than one element.
        # When left == right, we have found the minimum element.
        while left < right:
            # Calculate mid point.
            mid = left + (right - left) // 2
            
            # Compare the middle element with the rightmost element.
            # This comparison helps determine which part of the array contains the pivot.
            if nums[mid] > nums[right]:
                # If nums[mid] > nums[right], the pivot (minimum element) must be
                # in the right subarray (from mid + 1 to right).
                # The segment from left to mid is part of the larger values segment.
                left = mid + 1
            else:
                # If nums[mid] <= nums[right], the pivot is either at mid or
                # in the left subarray. The segment from mid to right is sorted.
                # We can't discard mid as it could be the minimum element itself.
                right = mid
        
        # When the loop terminates, left and right will point to the minimum element.
        return nums[left]

Find Minimum in Rotated Sorted Array in Java

class Solution {
    public int findMin(int[] nums) {
        int left = 0;
        int right = nums.length - 1;
        
        // The loop continues as long as the search space has more than one element.
        // When left == right, we have found the minimum element.
        while (left < right) {
            // Calculate mid point to avoid potential overflow.
            int mid = left + (right - left) / 2;
            
            // Compare the middle element with the rightmost element.
            // This comparison helps determine which part of the array is sorted.
            if (nums[mid] > nums[right]) {
                // If nums[mid] > nums[right], it means the pivot (minimum element)
                // is in the right half of the current search space.
                // The portion from left to mid is sorted and all elements are larger
                // than the elements in the right part.
                // So, we discard the left half including mid.
                left = mid + 1;
            } else {
                // If nums[mid] <= nums[right], it means the pivot is either at mid
                // or in the left half of the current search space.
                // The portion from mid to right is sorted.
                // We cannot discard mid as it could be the minimum element itself.
                // So, we shrink the search space to the left half including mid.
                right = mid;
            }
        }
        
        // At the end of the loop, left and right pointers converge to the minimum element.
        return nums[left];
    }
}

Find Minimum in Rotated Sorted Array in C++

class Solution {
public:
    int findMin(vector<int>& nums) {
        int left = 0;
        int right = nums.size() - 1;
        
        // The loop continues as long as the search space has more than one element.
        // When left == right, we have found the minimum element.
        while (left < right) {
            // Calculate mid point to avoid potential overflow.
            int mid = left + (right - left) / 2;
            
            // Compare the middle element with the rightmost element.
            // This comparison helps determine which part of the array is sorted.
            if (nums[mid] > nums[right]) {
                // If nums[mid] > nums[right], it means the pivot (minimum element)
                // is in the right half of the current search space.
                // The portion from left to mid is sorted and all elements are larger
                // than the elements in the right part.
                // So, we discard the left half including mid.
                left = mid + 1;
            } else {
                // If nums[mid] <= nums[right], it means the pivot is either at mid
                // or in the left half of the current search space.
                // The portion from mid to right is sorted.
                // We cannot discard mid as it could be the minimum element itself.
                // So, we shrink the search space to the left half including mid.
                right = mid;
            }
        }
        
        // At the end of the loop, left and right pointers converge to the minimum element.
        return nums[left];
    }
};

Find Minimum in Rotated Sorted Array in TypeScript

function findMin(nums: number[]): number {
    let left = 0;
    let right = nums.length - 1;

    // The loop continues as long as the search space has more than one element.
    // When left === right, we have found the minimum element.
    while (left < right) {
        // Calculate mid point to avoid potential overflow.
        const mid = left + Math.floor((right - left) / 2);

        // Compare the middle element with the rightmost element.
        // This comparison helps determine which part of the array contains the pivot.
        if (nums[mid] > nums[right]) {
            // If nums[mid] > nums[right], the pivot (minimum element) must be
            // in the right subarray (from mid + 1 to right).
            // The segment from left to mid is part of the larger values segment.
            left = mid + 1;
        } else {
            // If nums[mid] <= nums[right], the pivot is either at mid or
            // in the left subarray. The segment from mid to right is sorted.
            // We can't discard mid as it could be the minimum element itself.
            right = mid;
        }
    }

    // When the loop terminates, left and right will point to the minimum element.
    return nums[left];
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.