Try the Question
Before reading the solution, try solving the problem yourself:
👉 https://leetcode.com/problems/search-in-rotated-sorted-array/
Attempting the problem first helps build strong algorithmic intuition, which is extremely valuable during coding interviews.
Problem Statement
You are given an integer array nums that was originally sorted in ascending order with distinct elements.
Example of a sorted array:
Before the array is provided to the function, it may have been rotated at some pivot index k.
After rotation, the array structure becomes:
For example:
You are also given an integer target.
The goal is to return the index of the target element in the array.
If the element does not exist, return:
Important Constraint
The algorithm must run in O(log n) time complexity, which strongly suggests using Binary Search.
Example Walkthrough
Example 1
Input
Output
Explanation:
Example 2
Input
Output
Explanation:
Example 3
Input
Output
Understanding the Core Challenge
If the array were fully sorted, the problem would be straightforward because binary search could directly be applied.
Example:
However, due to rotation:
the array is no longer globally sorted.
But an important observation makes the problem solvable.
Key Observation
Even after rotation:
For example:
This property allows the use of binary search with additional conditions.
Approach 1 — Linear Scan (Brute Force)
The simplest method is to iterate through the entire array and check each element.
Algorithm
- Traverse the array from start to end.
- Compare every element with the target.
- Return the index if found.
Code
Time Complexity
Space Complexity
Although simple, this solution does not satisfy the required O(log n) complexity.
Approach 2 — Modified Binary Search
A better solution uses binary search with sorted half detection.
Idea
At every step:
- Calculate the middle index.
- Determine which half of the array is sorted.
- Check if the target lies inside that sorted half.
- Adjust the search range accordingly.
Implementation
Time Complexity
Space Complexity
This is the most common interview solution.
Approach 3 — Find Rotation Point Then Apply Binary Search
Another elegant strategy is to first locate the minimum element in the rotated array, which represents the rotation index (pivot).
Once the pivot is known, the array can be logically split into two sorted subarrays.
Example:
Two sorted sections exist:
After identifying the pivot:
- Decide which part may contain the target.
- Apply standard binary search on that portion.
Step 1 — Finding the Minimum Element (Rotation Pivot)
The smallest element indicates where the rotation happened.
Function to Find Minimum Value
Why This Works
If:
the minimum element must be on the right side.
Otherwise, it lies on the left side (including mid).
Step 2 — Standard Binary Search
After determining which half contains the target, a normal binary search is applied.
Complete Solution (Pivot + Binary Search)
Time Complexity
Finding pivot:
Binary search:
Total complexity:
Space Complexity
No additional memory is used.
Key Takeaways
✔ The array is sorted but rotated
✔ A rotation creates two sorted sections
✔ Binary search can still be applied
✔ Either detect the sorted half directly or locate the pivot first
✔ Both optimized approaches achieve O(log n) complexity
Final Thoughts
This problem is a classic binary search variation frequently asked in coding interviews. It evaluates the ability to:
- Recognize structural patterns in arrays
- Adapt binary search to non-standard conditions
- Maintain optimal algorithmic complexity
Understanding this pattern also helps solve related problems such as:
- Find Minimum in Rotated Sorted Array
- Search in Rotated Sorted Array II
- Find Rotation Count in Array
Mastering these concepts significantly strengthens binary search problem-solving skills for technical interviews.




