Suppose an array of length n sorted in ascending order is rotated between 1 and n times. Given the rotated array nums (all unique elements), find and return the minimum element in O(log n) time.
lo=0, hi=n-1. The minimum is somewhere in [lo..hi].mid = lo + (hi-lo)/2.nums[mid] > nums[hi]: drop is in right half → lo = mid+1.hi = mid.lo == hi: nums[lo] is the minimum.A rotated sorted array has exactly one inflection point — the rotation pivot — where values drop. Comparing nums[mid] to nums[hi] tells us which half contains the drop:
nums[mid] > nums[hi]: the left half is "normally sorted" but the big values are there. The drop must be to the right.
nums[mid] ≤ nums[hi]: the right half is sorted (or equal). The minimum is at mid or further left.
We never compare to nums[lo] because the pivot could be exactly at mid.
Scanning every element is O(n). The rotation leaves two sorted runs, and the minimum is where the second run starts.
Compare the middle with the last element of the range. If the middle is bigger, the drop happens to the right, so the minimum is on the right. Otherwise, the minimum is at mid or to its left.
Compare mid with the right end to see which side holds the drop. The minimum is the start of the second sorted run.
Run the animation above on your own input, or watch the 1:12 video explanation of Find Minimum in Rotated Sorted Array with a dry run and Java code.