InferenceAI

Enter at least two characters to search.

Start free

Search a Rotated List

mediumBinary Search25 min25 XP

A sorted list of distinct integers was rotated at an unknown pivot (for example [0,1,2,4,5,6,7] became [4,5,6,7,0,1,2]). Return the index of target, or -1.

Requirements

  • Run in O(log⁡n)O(\log n) time.

Examples

EXAMPLE WALKTHROUGHright half

A guided view of the published example, not a trace of your code.

INPUT[[4,5,6,7,0,1,2],0]
TASKsearch_rotated_list()
EXPECTED OUTPUT4

Start with the input values shown in this example.

Example 1 of 3

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

Output: 4

Explanation: right half

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

Output: -1

Explanation: absent

Input: nums = [1], target = 0

Output: -1

Explanation: single

Hint 1

At every step, one half around the middle is still sorted.

Hint 2

Check whether the target lies inside the sorted half.

Topics

DSABinary SearchPython