Skip to main content

Search in Rotated Sorted Array

MediumBinary Search

A sorted array has been rotated at some pivot (e.g., [0,1,2,4,5,6,7] becomes [4,5,6,7,0,1,2]). Given the rotated array nums and a target, return the index of target or -1 if not found. You must write an algorithm with O(log n) runtime.

Example:

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

Constraints:

  • 1 <= nums.length <= 5000
  • -10^4 <= nums[i] <= 10^4
  • All values of nums are unique
  • nums was sorted in ascending order and then rotated