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(int* nums, int numsSize) {
int left = 0, right = numsSize - 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 range is reduced to one element, roughly log base 2 of the array size.
Each step cuts the search area 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 extra step, showing slow growth.
Time Complexity: O(log n)
This means the time to find the minimum grows slowly, increasing by one step each time the array size doubles.
[X] Wrong: "The search takes linear time because the array is rotated and not fully sorted."
[OK] Correct: The array is still partially sorted, allowing binary search to work and find the minimum quickly without checking every element.
Understanding this pattern helps you show how to use binary search in tricky situations, a skill valued in many coding challenges.
"What if the array was not rotated but fully sorted? How would the time complexity change?"
Practice
[4, 5, 6, 7, 0, 1, 2]?Solution
Step 1: Understand the rotated sorted array
A rotated sorted array is originally sorted but then shifted. The smallest element is where the rotation happened.Step 2: Identify the minimum element
The minimum element is the smallest number and marks the rotation point in the array.Final Answer:
To find the smallest element which is the rotation point -> Option AQuick Check:
Minimum element = rotation point [OK]
- Looking for the largest element instead
- Trying to count rotations instead of finding minimum
- Sorting the array again unnecessarily
nums of size n?Solution
Step 1: Set pointers to array bounds
In C, array indices start at 0, so left should be 0 and right should be n-1.Step 2: Declare mid variable
Mid is used to find the middle index between left and right.Final Answer:
int left = 0, right = n - 1; int mid; -> Option CQuick Check:
Array indices start at 0, so left=0 and right=n-1 [OK]
- Setting left or right outside valid index range
- Using right = n which is out of bounds
- Starting left at 1 instead of 0
int nums[] = {4, 5, 6, 7, 0, 1, 2};
int left = 0, right = 6, mid;
while (left < right) {
mid = left + (right - left) / 2;
if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
right = mid;
}
}
printf("%d", nums[left]);Solution
Step 1: Understand the binary search logic
The code compares middle element with right element to decide which half contains the minimum.Step 2: Trace the loop to find minimum
Loop narrows down to index 4 where value is 0, the smallest element.Final Answer:
0 -> Option BQuick Check:
Minimum element found = 0 [OK]
- Printing nums[mid] instead of nums[left]
- Confusing greater than and less than in condition
- Not updating left or right correctly
nums of size n:
int left = 0, right = n - 1, mid;
while (left <= right) {
mid = left + (right - left) / 2;
if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return nums[left];Solution
Step 1: Analyze loop condition
Using left <= right can cause infinite loop or out-of-bound access because left can surpass right.Step 2: Correct loop condition
Changing to left < right ensures the loop stops when left meets right, correctly locating minimum.Final Answer:
The loop condition should be left < right, not left <= right -> Option DQuick Check:
Loop condition left < right avoids infinite loops [OK]
- Using <= in loop condition causing infinite loops
- Incorrectly adjusting right pointer by mid - 1
- Returning nums[left] without proper loop exit
nums that may contain duplicates, which modification to the binary search approach helps correctly find the minimum element?
A) Always move left pointer when nums[mid] == nums[right]
B) Increase left pointer by one when nums[mid] == nums[right]
C) Decrease right pointer by one when nums[mid] == nums[right]
D) Return nums[mid] immediately when nums[mid] == nums[right]Solution
Step 1: Understand duplicates in rotated array
Duplicates can cause ambiguity when nums[mid] equals nums[right], so normal binary search fails.Step 2: Adjust right pointer carefully
When nums[mid] == nums[right], decreasing right by one helps skip duplicate safely without missing minimum.Final Answer:
Decrease right pointer by one when nums[mid] == nums[right] -> Option AQuick Check:
Handle duplicates by reducing right pointer [OK]
- Moving left pointer instead causing missed minimum
- Returning mid value prematurely
- Not handling duplicates causing wrong answer
Ready to prove it?
Pass the test and this pattern moves unknown
