FastPrepMinimum in a Rotated Sorted Array

Minimum in a Rotated Sorted Array

Mygate logoMygate● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Given a nonempty array nums formed by rotating a strictly increasing integer array, return its minimum element.

Rotating moves a prefix to the end while preserving the order within each part. Zero rotations are allowed. All elements are distinct.

Your solution must use O(log n) time and O(1) extra space.

Function

minimumRotatedArray(nums: int[]) → int

Examples

Example 1

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

The increasing sequence [1, 2, 3, 4, 5] was rotated, and its minimum remains 1.

Example 2

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

A zero rotation is valid; the first value is the minimum.

Example 3

nums = [5]return = 5

A singleton has no different minimum.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • All values are distinct, and nums is a rotation of a strictly increasing array.

More Mygate problems

See Mygate hiring insights
public int minimumRotatedArray(int[] nums) {
    // write your code here
}
nums[3,4,5,1,2]
expected1
Checking account…