mediumBinary SearchBinary Search

Search in Rotated Sorted Array

mediumTime: O(log n)Space: O(1)

Recognize the pattern

sorted but rotatedfind the target by cutting the range in halfone half is always sorted

Brute force idea

If you approach Search in Rotated Sorted Array in the most literal way possible, you get this: Linear scan. It is a fair place to begin because it matches the surface of the question, yet it does not capture the deeper structure that makes the problem simpler.

Better approach

A calmer way to see Search in Rotated Sorted Array is this: Modified binary search: determine which half is sorted, check if target is in that half. The goal is not to be clever for its own sake, but to remember the one relationship that keeps the solution grounded as you move forward.

Key invariant

The truth you want to protect throughout Search in Rotated Sorted Array is this: At least one half of the array around mid is always sorted after rotation. If that remains true after every update, the rest of the reasoning has a stable place to stand.

Watch out for

The trap in Search in Rotated Sorted Array usually looks like this: Not handling the case where target equals the boundary element of the sorted half. When the code becomes mechanical before the idea is clear, small edge cases start breaking the whole story.

Binary Search Pattern