Search in Rotated Sorted Array in Olog n time
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Searching a rotated sorted array in O(log n) time is a classic binary-search variant. The trick is that although the whole array is not globally sorted, at least one half of the current search interval is always sorted.
Use Binary Search with a Sorted-Half Check
Consider a rotated array such as [4, 5, 6, 7, 0, 1, 2]. At any midpoint, either the left half or the right half is still ordered normally. Once you know which half is sorted, you can decide whether the target belongs there.
This keeps the search logarithmic because each step discards half of the remaining range.
Why the Algorithm Works
In a rotated sorted array without duplicates, the pivot splits the data into two ascending segments. For any interval left..right, the midpoint must sit in one of those segments. That means one of the following is true:
- '
nums[left] <= nums[mid], so the left side is sorted' - otherwise the right side is sorted
Once you identify the sorted half, checking whether the target falls inside its bounds tells you which side can be discarded safely. That is the same elimination principle that makes ordinary binary search efficient.
The iterative version also keeps space complexity at O(1) because it stores only index boundaries. A recursive version is logically equivalent, but it adds call-stack overhead without improving the time complexity.
Handle Duplicates as a Separate Variant
If duplicates are allowed, the sorted-half test can become ambiguous when nums[left] == nums[mid] == nums[right]. In that case, shrink the boundaries cautiously.
This works, but the worst case can degrade toward linear time because duplicates can hide the ordering information.
Common Pitfalls
The biggest mistake is using ordinary binary search without checking which half is sorted. Rotation breaks the assumption that the entire interval is globally ordered.
Another common issue is getting the comparison bounds slightly wrong, especially around <= versus <. Those off-by-one mistakes often fail only on pivot-adjacent targets.
People also forget to test edge cases such as:
- single-element arrays
- arrays that were not rotated at all
- targets at the pivot
- targets that are absent
Finally, if the problem statement allows duplicates, do not assume the no-duplicates version still guarantees O(log n) in every case.
Summary
- This problem is solved with a modified binary search.
- At each step, one half of the interval is still sorted.
- Use the sorted half to decide whether to keep the left side or the right side.
- The no-duplicates version runs in
O(log n). - With duplicates, the logic becomes more ambiguous and can degrade toward linear time.
Related reading
- Search ranking/relevance algorithms
- Searching a tree using LINQ
- Searching for a fast/efficient histogram algorithm with pre-specified bins
- Searching for an element in a circular sorted array
- Searching in a sorted and rotated array
- Second max in BST
- search text in dynamodb, break up tables
- Searching for an element in logn time

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.