Given a sorted array of distinct values that has been rotated between 1 and n times, return its minimum element in O(log n).
Before you code
The minimum is right after the drop. Comparing a[mid] with a[hi] tells you which side of mid the drop is on, and also handles an array that is not rotated at all.
Hints
- If
a[mid] > a[hi], the drop is after mid:lo = mid + 1. - Otherwise
a[mid..hi]is sorted and the minimum is at mid or before:hi = mid. - Loop while
lo < hi; the answer isa[lo].
Production & complexity notes
When solving Find minimum in rotated sorted array on LeetCode (medium), always state your loop or recursion invariant before writing code. Verify how your chosen data structure behaves on degenerate inputs (single-element inputs, all-duplicate keys, strictly monotonic sequences, or maximum constraint sizes up to $10^5$ elements). In production interviews, pair your implementation with an explicit time and auxiliary space complexity proof, explain why alternative brute-force approaches violate the latency budget, and trace how cache locality and memory allocation overhead affect wall-clock throughput on real hardware. Review the step-by-step memory trace in Rotated and two-dimensional data to reinforce the underlying pattern.
What it practices
Finding the rotation point, from Rotated and two-dimensional data.