Skip to main content

86. Find Minimum in Rotated Sorted Array

mediumAsked at Ola

Find the minimum element of a rotated sorted array with distinct values.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Problem

Suppose an array of length n sorted in ascending order is rotated between 1 and n times. Given the sorted rotated array nums of unique elements, return the minimum element of this array in O(log n).

Constraints

  • n == nums.length
  • 1 <= n <= 5000
  • -5000 <= nums[i] <= 5000
  • All integers are unique

Examples

Example 1

Input
nums = [3,4,5,1,2]
Output
1

Example 2

Input
nums = [4,5,6,7,0,1,2]
Output
0

Approaches

1. Linear scan

Min over the whole array.

Time
O(n)
Space
O(1)
return Math.min(...nums);

Tradeoff:

2. Binary search by right end

Compare nums[mid] with nums[hi]; the rotation point lies in the half where the value drops.

Time
O(log n)
Space
O(1)
function findMin(nums) {
  let lo = 0, hi = nums.length - 1;
  while (lo < hi) {
    const mid = (lo + hi) >> 1;
    if (nums[mid] > nums[hi]) lo = mid + 1;
    else hi = mid;
  }
  return nums[lo];
}

Tradeoff:

Ola-specific tips

Ola interviewers like rotation-aware binary search; tie it to finding the lowest surge value in a wrap-around hourly fare table.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Practice these live with InterviewChamp.AI

Drill Find Minimum in Rotated Sorted Array and other Ola interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →