Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Search in a rotated sorted array

Find the index of a target in a rotated sorted array using O(log n) time.

Python practice14 minSearching & SortingIntermediateLast updated March 25, 2026

Problem statement

You are given an array that was originally sorted in ascending order and then rotated at an unknown pivot. Implement a function that returns the index of a given target value in the array. If the target is not present, return -1. The array contains unique integers.

Task

Use a modified binary search to locate a target in a rotated sorted array without scanning linearly.

Examples

Basic rotated search

Input

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

Output

4

0 appears at index 4 in the rotated array.

Input format

Two arguments: nums (list of unique integers), target (integer).

Output format

An integer index of target in nums, or -1 if not found.

Constraints

Time complexity: O(log n). Space complexity: O(1). Array length n >= 0. All elements are unique.

Samples

Sample 1

Input

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

Output

4

3 is at index 4.