What if you could find anything fast even when the order looks broken?
Why Search in Rotated Sorted Array in DSA C++?
Imagine you have a long list of books sorted by title on a shelf. Suddenly, someone rotates part of the shelf, so the order is broken but still partially sorted. Now, you want to find a specific book quickly.
Looking for the book by checking each title one by one is slow and tiring. The usual quick search methods don't work well because the order is not fully sorted anymore, making it easy to get lost or miss the book.
This concept helps you find the book fast even if the shelf is rotated. It cleverly checks parts of the shelf to decide where to look next, skipping large sections and saving time.
for (int i = 0; i < n; i++) { if (arr[i] == target) return i; } return -1;
int left = 0, right = n - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) return mid; // decide which half is sorted and search accordingly }
You can quickly find any item in a rotated sorted list without checking every element.
Finding a specific time in a daily schedule that was shifted due to daylight saving time, without scanning every hour.
Manual search is slow on rotated sorted lists.
Binary search adapts to find the target efficiently.
This method saves time by skipping unnecessary checks.
Practice
Solution
Step 1: Understand normal binary search assumption
Binary search works efficiently on fully sorted arrays by comparing mid element to target.Step 2: Identify the problem with rotated arrays
Rotated sorted arrays are sorted but shifted, so the order is broken at the rotation point, making direct binary search invalid.Final Answer:
The array is not fully sorted, so normal binary search cannot be directly applied. -> Option AQuick Check:
Rotated array breaks sorted order = need modified search [OK]
- Assuming array is fully sorted and applying normal binary search
- Ignoring rotation and searching linearly
- Confusing duplicates with rotation
Solution
Step 1: Identify loop type for binary search
Binary search uses a while loop with condition start <= end to narrow search range.Step 2: Check options for correct loop syntax
while (start <= end) uses while (start <= end), which is standard for binary search loops.Final Answer:
while (start <= end) -> Option CQuick Check:
Binary search loop uses while (start <= end) [OK]
- Using for loop instead of while for binary search
- Using incorrect loop condition like start < end
- Confusing if statement with loop
int arr[] = {4,5,6,7,0,1,2};
int target = 0;
int start = 0, end = 6;
int result = -1;
while (start <= end) {
int mid = start + (end - start) / 2;
if (arr[mid] == target) {
result = mid;
break;
}
if (arr[start] <= arr[mid]) {
if (target >= arr[start] && target < arr[mid])
end = mid - 1;
else
start = mid + 1;
} else {
if (target > arr[mid] && target <= arr[end])
start = mid + 1;
else
end = mid - 1;
}
}
std::cout << result << std::endl;Solution
Step 1: Trace initial variables and mid calculation
start=0, end=6, mid=3 (element 7). arr[mid] != target (0).Step 2: Check which half is sorted and adjust search
arr[start]=4 <= arr[mid]=7, left half sorted. Target 0 not in [4,7), so search right half: start=mid+1=4.Step 3: Next iteration mid=5 (element 1)
arr[mid]=1 != target. arr[start]=0 <= arr[mid]=1, left half sorted. Target 0 in [0,1), so end=mid-1=4.Step 4: Next iteration mid=4 (element 0)
arr[mid]=0 == target, result=4, break loop.Final Answer:
4 -> Option DQuick Check:
Target 0 found at index 4 [OK]
- Not updating start/end correctly based on sorted half
- Confusing indices and returning wrong index
- Missing break after finding target
int search(int arr[], int n, int target) {
int start = 0, end = n - 1;
while (start < end) {
int mid = start + (end - start) / 2;
if (arr[mid] == target) return mid;
if (arr[start] <= arr[mid]) {
if (target >= arr[start] && target < arr[mid])
end = mid - 1;
else
start = mid + 1;
} else {
if (target > arr[mid] && target <= arr[end])
start = mid + 1;
else
end = mid - 1;
}
}
return -1;
}Solution
Step 1: Analyze loop condition impact
Using while (start < end) misses checking when start == end, possibly skipping last element.Step 2: Correct loop condition for complete search
Changing to while (start <= end) ensures all elements are checked, including when start equals end.Final Answer:
The loop condition should be while (start <= end) instead of while (start < end). -> Option BQuick Check:
Loop must cover start == end to check last element [OK]
- Using while (start < end) and missing last element
- Ignoring overflow in mid calculation (not in this code though)
- Incorrect comparison operators
arr = {15, 18, 2, 3, 6, 12}, write a C++ function to find the index of the minimum element. What is the output of your function?Solution
Step 1: Understand the minimum element in rotated array
The minimum element is the rotation point where order breaks. Here, 2 at index 2 is smallest.Step 2: Use binary search to find minimum
Check mid and compare with end to decide which half contains minimum. Eventually, index 2 is found.Final Answer:
2 -> Option AQuick Check:
Minimum element 2 is at index 2 [OK]
- Returning first element index instead of minimum
- Confusing maximum with minimum element
- Not handling rotation correctly
Ready to prove it?
Pass the test and this pattern moves unknown
