> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Find Minimum in Rotated Sorted Array

> Tested Python solution for LeetCode 153 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 153, Medium. Topics: Array, Binary Search. [View on LeetCode](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 153   # by problem number
lcpy gen -s find_minimum_in_rotated_sorted_array   # by problem name
```

## Problem

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.

### Examples

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

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

```
Input: nums = [11,13,15,17]
Output: 11
Explanation: The original array was [11,13,15,17] and it was rotated 4 times.
```

### Constraints

* n == nums.length
* 1 \<= n \<= 5000
* -5000 \<= nums\[i] \<= 5000
* All the integers of nums are **unique**.
* nums is sorted and rotated between 1 and n times.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/find_minimum_in_rotated_sorted_array/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/find_minimum_in_rotated_sorted_array/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(log n) - binary search
    # Space: O(1) - only using constant extra space
    def find_min(self, nums: list[int]) -> int:
        """
        Find the minimum element in a rotated sorted array using binary search.

        The key insight is that in a rotated sorted array, one half is always sorted.
        We can determine which half contains the minimum by comparing the middle
        element with the rightmost element.

        Algorithm:
        1. If nums[left] < nums[right], the array is not rotated, return nums[left]
        2. Otherwise, find the rotation point using binary search
        3. The minimum is always at the rotation point
        """
        left, right = 0, len(nums) - 1

        # If the array is not rotated, the first element is the minimum
        if nums[left] < nums[right]:
            return nums[left]

        # Binary search to find the rotation point
        while left < right:
            mid = left + (right - left) // 2

            # If mid element is greater than right element,
            # the rotation point is in the right half
            if nums[mid] > nums[right]:
                left = mid + 1
            else:
                # If mid element is less than or equal to right element,
                # the rotation point is in the left half (including mid)
                right = mid

        return nums[left]
```

## Complexity

| Time                     | Space                                  |
| ------------------------ | -------------------------------------- |
| O(log n) - binary search | O(1) - only using constant extra space |

## Tags

[Grind](/catalog/grind), [Blind 75](/catalog/blind-75), [NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
