Search in Rotated Sorted Array in DSA C++ - Time & Space Complexity
We want to know how fast we can find a number in a rotated sorted list.
How does the search time grow as the list gets bigger?
Analyze the time complexity of the following code snippet.
int search(vector<int>& nums, int target) {
int left = 0, right = nums.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target) return mid;
if (nums[left] <= nums[mid]) {
if (nums[left] <= target && target < nums[mid]) right = mid - 1;
else left = mid + 1;
} else {
if (nums[mid] < target && target <= nums[right]) left = mid + 1;
else right = mid - 1;
}
}
return -1;
}
This code searches for a target number in a rotated sorted array using a modified binary search.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that halves the search range each time.
- How many times: At most, it runs about log base 2 of n times, where n is the array size.
Each step cuts the search area roughly in half, so the number of steps grows slowly as the list grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 4 steps |
| 100 | About 7 steps |
| 1000 | About 10 steps |
Pattern observation: Doubling the input size adds only one more step to the search.
Time Complexity: O(log n)
This means the search time grows slowly, making it efficient even for large lists.
[X] Wrong: "Because the array is rotated, we must check every element, so it is O(n)."
[OK] Correct: The code cleverly uses the sorted parts to skip half the array each time, keeping the search fast.
Understanding this search shows you can adapt classic methods to tricky situations, a skill valued in problem solving.
"What if the array was not rotated but still sorted? How would the time complexity change?"
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
