Find Minimum in Rotated Sorted Array in DSA C++ - Time & Space Complexity
We want to understand how fast we can find the smallest number in a rotated sorted array.
How does the time needed change as the array gets bigger?
Analyze the time complexity of the following code snippet.
int findMin(const std::vector<int>& nums) {
int left = 0, right = nums.size() - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
right = mid;
}
}
return nums[left];
}
This code finds the minimum element by using a binary search approach on a rotated sorted array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that halves the search range each time.
- How many times: The loop runs until the search space is reduced to one element, about log base 2 of n times.
Each step cuts the array roughly in half, so the number of steps grows slowly as the array 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, showing slow growth.
Time Complexity: O(log n)
This means the time to find the minimum grows slowly, even if the array becomes very large.
[X] Wrong: "Since the array is rotated, we must check every element to find the minimum, so it takes O(n) time."
[OK] Correct: The binary search approach uses the sorted property cleverly to skip half the elements each time, so it does not need to check all elements.
Understanding this pattern helps you show how to use binary search in clever ways beyond simple sorted arrays, 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 the rotated sorted array property
A rotated sorted array is sorted but rotated at some pivot point, causing a break in order.Step 2: Use binary search to find the break point
Binary search helps find the pivot where the order breaks, which is the minimum element.Final Answer:
Use binary search to find the point where the order breaks -> Option DQuick Check:
Binary search finds minimum in rotated array [OK]
- Trying to sort the array again wastes time
- Linear search is slower than binary search
- Using extra space unnecessarily
nums?Solution
Step 1: Understand binary search loop conditions
For finding minimum in a rotated sorted array, the binary search loop typically uses while (left < right) to avoid infinite loops and correctly narrow down the search space.Step 2: Check correct mid calculation and loop condition
while (left < right) { int mid = (left + right) / 2; } is the common and correct approach for this problem.Final Answer:
while (left < right) { int mid = (left + right) / 2; } -> Option BQuick Check:
Correct binary search loop syntax for this problem [OK]
- Using for loop instead of while for binary search
- Calculating mid as left + (right - left) / 2 with wrong loop condition
- Using wrong loop condition like left <= right causing infinite loop
std::vector<int> nums = {4,5,6,7,0,1,2};
int left = 0, right = nums.size() - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] > nums[right]) left = mid + 1;
else right = mid;
}
std::cout << nums[left] << std::endl;Solution
Step 1: Understand the binary search logic on rotated array
The code compares nums[mid] with nums[right] to decide which half to keep.Step 2: Trace the loop to find minimum
Since nums[mid] > nums[right], left moves to mid+1 until left == right at index of minimum (0).Final Answer:
0 -> Option AQuick Check:
Minimum element found is 0 [OK]
- Confusing mid and right comparison direction
- Printing nums[right] instead of nums[left]
- Not updating left or right correctly
int findMin(std::vector<int>& nums) {
int left = 0, right = nums.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] > nums[right]) left = mid + 1;
else right = mid - 1;
}
return nums[left];
}Solution
Step 1: Analyze the binary search update rules
When nums[mid] <= nums[right], the minimum is at mid or left side, so right should be set to mid.Step 2: Identify incorrect update causing skipping minimum
Setting right = mid - 1 skips mid, possibly missing the minimum element.Final Answer:
The else block should set right = mid, not right = mid - 1 -> Option AQuick Check:
Correct right update avoids missing minimum [OK]
- Using left <= right causes infinite loop
- Returning nums[left] is correct
- Mid calculation is safe here
nums that may contain duplicates, which modification is needed in the binary search approach to correctly find the minimum element?Solution
Step 1: Understand impact of duplicates on binary search
Duplicates can cause ambiguity when nums[mid] == nums[right], making it unclear which side to choose.Step 2: Modify search by reducing right to skip duplicate
Reducing right by one helps shrink the search space safely without missing the minimum.Final Answer:
If nums[mid] == nums[right], reduce right by one to skip duplicate -> Option CQuick Check:
Handle duplicates by shrinking right pointer [OK]
- Returning immediately on equality misses minimum
- Increasing left instead of reducing right causes errors
- Ignoring duplicates leads to wrong answers
Ready to prove it?
Pass the test and this pattern moves unknown
