Skip to content

Search in Rotated Sorted Array

MediumBinary Search
Decode it first. Clues, constraints, pattern and plan before any code.

A list of distinct integers was sorted in ascending order and then rotated at an unknown point (for example [0, 1, 2, 4, 5, 6, 7] might become [4, 5, 6, 7, 0, 1, 2]). Given the rotated list nums and a target, return the index of target, or -1 if it is not there. Your solution must run in O(log n) time.

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

Constraints

  • 1 <= nums.length <= 5000
  • -10^4 <= nums[i], target <= 10^4
  • All values in nums are unique.