progress

~/problems / Binary search / Binary search

Find Minimum in Rotated Sorted Array

On a phone? Coding is easier on a laptop: email this problem to yourself . Meanwhile: quiz this topic or fight a boss.

medium ~25 min

Write find_min(nums: list[int]) -> int.

nums is a strictly increasing list of distinct values that has been rotated by an unknown amount (possibly 0): a prefix was moved to the end, e.g. [2, 5, 7, 9, 10] → [9, 10, 2, 5, 7]. Return the smallest value.

Example: [9, 10, 2, 5, 7] gives 2; [2, 5, 7, 9, 10] gives 2; [8] gives 8.

Constraints: 1 <= len(nums) <= 10^6, called many times on the same list.

⭐ Bonus: min(nums) or any scan still passes; a speed test earns a star at O(log n).

Show hint

compare the middle element with the last element of the current range. That tells you which side of mid the drop (and so the minimum) is on.

Topic: Binary search. lo/hi invariants, lower vs upper bound, rotated arrays, bisect.

Read the visual guide
0:00
Ctrl ' run · Ctrl ↵ submit
esc