Search a Rotated List

MediumBinary Search≈ 35 min

A list of distinct integers was sorted from smallest to largest, then rotated: cut at some point, with the second part moved to the front. [1, 3, 5, 8, 9, 12] rotated at 8 becomes [8, 9, 12, 1, 3, 5].

Given the rotated list nums and a target, return the index of target, or -1 if it is not in the list. You do not know where the cut was.

Examples

text
nums = [8, 9, 12, 1, 3, 5], target = 1   →  3
nums = [8, 9, 12, 1, 3, 5], target = 7   →  -1
nums = [4], target = 4                   →  0

Constraints

  • Between 1 and 100,000 distinct numbers, sorted and then rotated (possibly by zero).
  • Aim for O(log n): reading the list from the start is O(n).
Sound off. Turn on for key clicks and a chime when the tests pass.Inline suggestions off. Recommended while you are learning. Turn on to complete words as you type.
Console
›Run to see what your code prints

Run checks the 3 visible cases here, in your browser. Submit runs all 7 on the judge, 4 of them hidden, and measures how your solution scales.