FastPrepSearch a Rotated Sorted Array

Search a Rotated Sorted Array

NVIDIA logoNVIDIA● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

An integer array nums has distinct values. It was sorted in strictly increasing order, then possibly rotated without changing the circular order.

Return the zero-based index of target, or -1 when it is absent. The array may also remain in its original sorted order.

Function

search(nums: int[], target: int) → int

Examples

Example 1

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

The target occurs at zero-based index 4.

Example 2

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

The target does not occur in either sorted segment.

Example 3

nums = [1]target = 0return = -1

The only value differs from the target.

Constraints

  • 1 <= nums.length <= 5000.
  • -10^4 <= nums[i], target <= 10^4.
  • All values are distinct; nums is a rotation of an increasing array.

More NVIDIA problems

See NVIDIA hiring insights
public int search(int[] nums, int target) {
    return -1;
}
nums[4,5,6,7,0,1,2]
target0
expected4
Checking account…