Minimum in Rotated Sorted Array

Let's try our approach when the input is a rotated and sorted array. Difficulty Level: Medium

Problem statement

Suppose an array that was sorted in ascending order is rotated. For example, the array nums = [0,1,2,4,5,6,7] might become:

  • [4,5,6,7,0,1,2], if it is rotated 4 times.
  • [0,1,2,4,5,6,7], if it is rotated 7 times.

Notice the rotation of an array [nums[0], nums[1], nums[2], ..., nums[n-1]] 1 time results in the array [nums[n-1], nums[0], nums[1], nums[2], ..., nums[n-2]].

Given sorted and rotated array nums, return the minimum element in the array. Constraints:

  • 1nums.length50001 \leq nums.length \leq 5000
  • 5000nums[i]5000-5000 \leq nums[i] \leq 5000
  • All of the integers of nums are unique.
  • nums is sorted and rotated.

Example 1:

Input: [3,4,5,1,2]    
Output: 1    

Explanation: The original array was [1,2,3,4,5], and it was rotated 3 times.

Example 2:

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

Explanation: The original array was [0,1,2,4,5,6,7], and it was rotated 4 times.

Example 3:

Input: [11,13,15,17]    
Output: 11    

Explanation: The original array was [11,13,15,17], and it was rotated 4 times.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.