Search in Rotated Sorted Array
Problem Statement
You are given an array nums of distinct numbers. It was sorted from smallest to largest, and then possibly rotated: cut at some index k and the front part moved to the back. For example, [0, 1, 2, 4, 5, 6, 7] rotated at k = 3 becomes [4, 5, 6, 7, 0, 1, 2].
You are also given a number target. Return the index of target in nums, or -1 if it is not there. Your solution must run in O(log n) time.
A rotated sorted array nums of distinct integers, and an integer target.
The index of target in nums, or -1 if it is not present.
0 sits at index 4, right after the drop from 7.
3 is not in the array.
The only element is 1.
- β’ 1 β€ n β€ 5000
- β’ β10^4 β€ nums[i] β€ 10^4
- β’ All values in nums are distinct.
- β’ nums is a sorted array that may have been rotated.
- β’ β10^4 β€ target β€ 10^4
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Check every index until target is found
LC 153 finds the minimum's index; then a normal binary search on the right sorted piece
At each mid, one half is sorted; go into it only if target fits its range
Walk the array from left to right. Return the first index where nums[i] == target, or -1 at the end.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Binary Search translate into production architectures and low-level memory mechanics.