Introduction
Search in Rotated Sorted Array means:
- searching target element
inside rotated sorted array
efficiently
Rotated Array Example:
Original:[1,2,3,4,5,6,7]
Rotated:
[4,5,6,7,1,2,3]
Goal:
- find target index
without linear traversal
Example:
Array:[4,5,6,7,0,1,2]
Target:
0
Output:
4
Explanation:
Target 0 exists at index 4. This problem is one of the most important applications of:
Modified Binary SearchConstraints
1 <= Array Size <= 10^5 Approach : Modified Binary Search
Explanations:
Explanation:
The idea is:
- one half array
always remains sorted - determine correct half
using binary search logic
Steps:
- Find middle index.
- Check target match.
- Detect sorted half.
- Check target range.
- Move search space.
- Repeat until found.
Key Observation:
At least one side is always sorted. Conditions:
left <= middle → left half sorted middle <= right → right half sorted This approach:
- uses binary search
- avoids full traversal
Dry Run
Array:[4,5,6,7,0,1,2]
Middle:
7
Left half sorted:
[4,5,6,7]
Target:
0
Move right.
Middle:
1
Target found range identified.
Practice :