mediumBinary SearchBinary Search

Search in Rotated Sorted Array

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

Signals to notice

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

Brute force first

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.

The key insight

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.

What must stay true

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.

Easy way to go wrong

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