Recommended resources
The freely available resources this lesson will direct you to.
- Search in rotated sorted array - Leetcode 33 - Pythonwww.youtube.com
- BS-4. Search Element in Rotated Sorted Array - Iwww.youtube.com
- 33. Search in Rotated Sorted Array - In-Depth Explanationalgo.monster
- Algorithm: Rotated Array. Binary Search, With a Twist | by Max Beneke | JavaScript in Plain Englishjavascript.plainenglish.io
Hello! In our last few lessons, we've built a solid foundation in binary search, moving from finding exact values to locating boundaries with lower-bound and upper-bound searches. The common thread was that our binarySearch template relied on a fundamental property: the entire array was sorted.
Today, we're going to tackle a classic interview problem that challenges this assumption: searching in a rotated sorted array. Imagine an array like [0, 1, 2, 4, 5, 6, 7] that gets "rotated" to become [4, 5, 6, 7, 0, 1, 2]. It's no longer fully sorted, so our standard binary search won't work. Your goal for this lesson is to learn how to adapt binary search to solve this problem efficiently, specifically by learning to identify which half of the array remains ordered at each step.
The Problem with Rotation
A standard binary search works by comparing the target to the middle element and confidently eliminating one half of the array. For example, in a sorted array, if target < nums[mid], we know the target must be in the left half.
But in a rotated array like [4, 5, 6, 7, 0, 1, 2], this logic breaks. If our target is 0 and we pick a middle element 7 (at index 3), 0 < 7. A standard binary search would incorrectly discard the right half, where the 0 actually is.
So, how do we solve this in O(log n) time? We can't do a linear scan, as that would be too slow for typical interview constraints. The solution lies in a brilliant insight about the structure of a rotated array.
The Key Insight: One Half is Always Sorted
Even though the entire array isn't sorted, if you split it at any middle point, at least one of the two halves will be perfectly sorted.
- Consider
[4, 5, 6, 7, 0, 1, 2]. Let's saymidis at index 3 (value = 7).- The left half is
[4, 5, 6, 7]. This is sorted. - The right half is
[0, 1, 2]. This is also sorted.
- The left half is
- Let's take a different
mid. Ifmidis at index 5 (value = 1).- The left half is
[4, 5, 6, 7, 0, 1]. This is not sorted. - The right half is
[2]. This is (trivially) sorted.
- The left half is
This property is our foothold. If we can identify the sorted half, we can determine with certainty whether our target lies within its bounds. If it does, we search that half. If it doesn't, we know it must be in the other, unsorted half. In either case, we discard half the search space, preserving our O(log n) efficiency.
The following video from the "take U forward" channel provides an excellent explanation of why standard binary search fails and introduces this core idea of identifying the sorted half.
BS-4. Search Element in Rotated Sorted Array - I
Watch the segment from this video. It starts by showing why a direct binary search is problematic and then clearly states the main strategy: "identify the sorted half".
What to focus on: The reasoning for why simply comparing target to nums[mid] is not enough. The "key point" that you must first identify which of the two subarrays (left to mid, or mid to right) is sorted.
The Algorithm: A Modified Binary Search
Now that we have the core idea, let's formalize it into an algorithm. At each step of our binary search loop:
-
Calculate
mid. -
If
nums[mid]equals ourtarget, we're done. Returnmid. -
Identify the sorted half. We can do this with a simple comparison:
if (nums[left] <= nums[mid]).- If this is true, the left half (
[left...mid]) is sorted. - If false, the pivot point is somewhere in the left half, which means the right half (
[mid...right]) must be sorted.
- If this is true, the left half (
-
Decide where to search next.
-
Case 1: Left half is sorted (
nums[left] <= nums[mid]).- We ask: Is our
targetin the range of this sorted half? That is,nums[left] <= target < nums[mid]. - If YES: The target is in the left half. Discard the right:
right = mid - 1. - If NO: The target must be in the other (unsorted) half. Discard the left:
left = mid + 1.
- We ask: Is our
-
Case 2: Right half is sorted (
nums[left] > nums[mid]).- We ask: Is our
targetin the range of this sorted half? That is,nums[mid] < target <= nums[right]. - If YES: The target is in the right half. Discard the left:
left = mid + 1. - If NO: The target must be in the other (unsorted) half. Discard the right:
right = mid - 1.
- We ask: Is our
-
This logic guarantees that we correctly narrow down the search space at every step. This image provides a great visual for the first step of this logic.

The AlgoMonster article on this problem provides a very clear, step-by-step breakdown of this approach.
33. Search in Rotated Sorted Array - In-Depth Explanation
This article explains the intuition and presents the full algorithm.
Please read the Intuition and Solution Approach sections. Focus on how the comparison nums[left] <= nums[mid] is used to split the logic into two main cases, and how the target's position is checked within the identified sorted half.
Example Walkthrough and Implementation
Let's trace the algorithm with the example from the article: nums = [4, 5, 6, 7, 0, 1, 2], target = 0.
- Initial:
left = 0,right = 6. - Iteration 1:
mid = 3,nums[mid] = 7. Not the target.- Is the left half sorted?
nums[0] <= nums[3](i.e.,4 <= 7)? Yes. - Is
targetin the sorted left half?nums[0] <= target < nums[3](i.e.,4 <= 0 < 7)? No. - Search right:
left = mid + 1 = 4.
- Iteration 2:
left = 4,right = 6.mid = 5,nums[mid] = 1. Not the target.- Is the left half sorted?
nums[4] <= nums[5](i.e.,0 <= 1)? Yes. - Is
targetin the sorted left half?nums[4] <= target < nums[5](i.e.,0 <= 0 < 1)? Yes. - Search left:
right = mid - 1 = 4.
- Iteration 3:
left = 4,right = 4.mid = 4,nums[mid] = 0. This is the target! Returnmid, which is4.
The NeetCode video below provides another excellent visualization of this process, which can help solidify your understanding.
Search in rotated sorted array - Leetcode 33 - Python
Watch from this segment. It covers the core logic and walks through the same example, providing a dynamic view of how the pointers L and R move.
Note that the explanation combines some of the if conditions, but the underlying logic is the same as what we've just discussed.
Here is the TypeScript implementation that directly follows our algorithm.
function search(nums: number[], target: number): number {
let left = 0;
let right = nums.length - 1;
while (left <= right) {
const mid = left + Math.floor((right - left) / 2);
if (nums[mid] === target) {
return mid;
}
// Case 1: The left half [left...mid] is sorted.
if (nums[left] <= nums[mid]) {
// Check if target is in the sorted left half.
if (nums[left] <= target && target < nums[mid]) {
right = mid - 1;
} else {
// Target must be in the right half.
left = mid + 1;
}
}
// Case 2: The right half [mid...right] is sorted.
else {
// Check if target is in the sorted right half.
if (nums[mid] < target && target <= nums[right]) {
left = mid + 1;
} else {
// Target must be in the left half.
right = mid - 1;
}
}
}
return -1; // Target not found
}
Common Pitfalls
This problem is notorious for off-by-one errors and tricky boundary conditions. Given your expressed "algo phobia," being aware of these traps is half the battle. The AlgoMonster resource has an excellent section on this.
33. Search in Rotated Sorted Array - In-Depth Explanation
This section details the most common mistakes made when implementing this algorithm.
Read the section on Common Pitfalls. Pay special attention to: Point #2, regarding the use of <= versus <. The = is crucial for handling cases where left and mid are the same index. Point #3, which explains why the range checks use strict inequality for nums[mid] (target < nums[mid] or target > nums[mid]). This is because we already handled the target === nums[mid] case at the beginning of the loop.
Conclusion
In this lesson, you've learned how to adapt the powerful binary search algorithm to a non-obvious case: a rotated sorted array. By staying calm and reasoning about the structure of the data, you can find an invariant—that one half is always sorted—and exploit it to maintain O(log n) performance.
Here are the key takeaways:
- A rotated sorted array can be searched in
O(log n)time by modifying binary search. - The core insight is that for any
midpoint, either the left subarray (nums[left...mid]) or the right subarray (nums[mid...right]) is guaranteed to be sorted. - You can identify the sorted half by checking if
nums[left] <= nums[mid]. - Once the sorted half is identified, you check if the target lies within its range. If it does, you search that half; otherwise, you search the other half.
In our next lesson, we will continue this theme of searching in partially ordered data structures. We'll move from a 1D array to a 2D matrix that is sorted by row and column, and you'll learn another clever elimination strategy to find an element efficiently.