PIXELBANKv8.2.1
Menu

Find Minimum in Rotated Sorted Array

Given a sorted rotated array of unique elements nums, return the minimum element.

The array was originally sorted in ascending order, then rotated between 1 and n times. You must solve it in O(log n) time.

Example:

Input:
3,4,5,1,2
Output:
1
Reasoning:
  • The input array is 3,4,5,1,2, which was originally sorted in ascending order and then rotated.
  • We use a modified binary search algorithm to find the minimum element in O(logn)O(\log n) time, where nn is the number of elements in the array.
  • The algorithm compares the middle element with the rightmost element: since 5>25 > 2, the minimum element must be in the right half of the array.
  • We repeat this process with the right half 1,2 and find that the minimum element is 11, which is the final output.

Constraints:

  • 1 <= len(nums) <= 5000
  • -5000 <= nums[i] <= 5000
  • All values are unique
  • nums was sorted then rotated
Editor

Test Results

0/0
Run code to see test results.