3.8 Search in Rotated Sorted Array

3.8.1 Problem Metadata

3.8.2 Description

Given an ascending-sorted array that has been rotated at an unknown pivot, search for a target value. If found, return its index; otherwise return -1. The array contains no duplicates, and the algorithm must run in O(log n).

3.8.3 Examples

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

Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1

3.8.4 Constraints

  • 1 <= nums.length <= 10^4
  • -10^4 <= nums[i], target <= 10^4
  • All elements are distinct