FastPrepFind Minimum in Rotated Sorted Array

Find Minimum in Rotated Sorted Array

Google logoGoogle● MediumINTERNONSITE INTERVIEW
Learn

Problem statement

You are given an array nums containing distinct integers. Before it is passed to your function, the array was sorted in strictly increasing order and then rotated at an unknown pivot. It may also have been rotated by its full length, leaving it in increasing order.

Return the minimum value in nums.

Your algorithm must run in O(log n) time.

Function

findMinimumInRotatedArray(nums: int[]) → int

Examples

Example 1

nums = [3,4,5,1,2]return = 1

The increasing array [1,2,3,4,5] was rotated, and its minimum value is 1.

Example 2

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

The rotation point lies between 7 and 0, so the minimum is 0.

Example 3

nums = [11,13,15,17]return = 11

A full-length rotation leaves the array in increasing order, so its first value is the minimum.

Constraints

  • 1 <= nums.length <= 5000.
  • -5000 <= nums[i] <= 5000.
  • All values in nums are distinct.
  • nums is a rotation of a strictly increasing array.

More Google problems

See Google hiring insights
public int findMinimumInRotatedArray(int[] nums) {
    // Write your code here.
}
nums[3,4,5,1,2]
expected1
Checking account…