Find Minimum in Rotated Sorted Array in DSA Javascript - Time & Space Complexity
We want to know how fast we can find the smallest number in a rotated sorted list.
How does the time needed change as the list gets bigger?
Analyze the time complexity of the following code snippet.
function findMin(nums) {
let left = 0;
let right = nums.length - 1;
while (left < right) {
let mid = Math.floor((left + right) / 2);
if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
right = mid;
}
}
return nums[left];
}
This code finds the smallest number in a rotated sorted array by using a smart search that cuts the search space in half each time.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that compares middle and right elements to narrow down the search.
- How many times: The loop runs until the search space is reduced to one element, roughly halving each time.
Each step cuts the list roughly in half, so the number of steps grows slowly even if the list grows a lot.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 4 steps |
| 100 | About 7 steps |
| 1000 | About 10 steps |
Pattern observation: The steps grow very slowly compared to input size because the search space halves each time.
Time Complexity: O(log n)
This means the time to find the minimum grows slowly as the list gets bigger, making it very efficient.
[X] Wrong: "The search must check every number to find the minimum, so it takes O(n) time."
[OK] Correct: The code uses a smart method that cuts the search area in half each time, so it doesn't check every number.
Understanding this search method shows you can use smart ways to find answers quickly, a skill that helps in many coding problems.
"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
A rotated sorted array is a sorted array shifted at some pivot. The smallest element is the rotation point.Step 2: Identify the goal
The goal is to find the smallest element, which is the minimum after rotation.Final Answer:
To find the smallest element after the rotation point -> Option CQuick Check:
Minimum element = smallest after rotation [OK]
- Confusing minimum with maximum
- Trying to count rotations instead of finding minimum
- Assuming array is fully sorted without rotation
nums?Solution
Step 1: Identify the loop type for binary search
Binary search typically uses a while loop with condition left < right to narrow down the search space.Step 2: Check options
while(left < right) { ... } uses while(left < right), which is correct for binary search in this problem.Final Answer:
while(left < right) { ... } -> Option AQuick Check:
Binary search loop uses while(left < right) [OK]
- Using for loop incorrectly for binary search
- Using <= instead of < causing infinite loops
- Not initializing left and right properly
function findMin(nums) {
let left = 0, right = nums.length - 1;
while (left < right) {
let mid = Math.floor((left + right) / 2);
if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
right = mid;
}
}
return nums[left];
}
console.log(findMin([4,5,6,7,0,1,2]));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 code with input [4,5,6,7,0,1,2]
Initially left=0, right=6. Mid=3 (value 7). Since 7 > 2, left = mid+1 = 4. Now left=4, right=6. Mid=5 (value 1). 1 <= 2, so right=mid=5. Next left=4, right=5. Mid=4 (value 0). 0 <= 1, right=mid=4. Now left=4, right=4, loop ends. Return nums[4] = 0.Final Answer:
0 -> Option DQuick Check:
Minimum element found = 0 [OK]
- Returning nums[mid] instead of nums[left]
- Confusing comparison direction
- Not updating pointers correctly
function findMin(nums) {
let left = 0, right = nums.length - 1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
right = mid;
}
}
return nums[left];
}Solution
Step 1: Analyze loop condition
The loop uses left <= right, which can cause infinite loop or skip the correct minimum index.Step 2: Correct loop condition
Binary search for minimum in rotated array requires left < right to narrow search correctly without missing elements.Final Answer:
The loop condition should be left < right, not left <= right -> Option AQuick Check:
Use while(left < right) for correct binary search [OK]
- Using <= causing infinite loops
- Changing mid calculation unnecessarily
- Returning wrong index after loop
nums that may contain duplicates, which modification is needed in the binary search approach to correctly find the minimum?Solution
Step 1: Understand duplicates effect
Duplicates can cause ambiguity when nums[mid] equals nums[right], making it unclear which side to search.Step 2: Modify binary search to handle duplicates
When nums[mid] == nums[right], safely reduce right by 1 to skip duplicate and continue search.Final Answer:
When nums[mid] == nums[right], reduce right by 1 to skip duplicate -> Option BQuick Check:
Handle duplicates by shrinking right pointer [OK]
- Ignoring duplicates causing wrong minimum
- Moving left pointer incorrectly on equality
- Sorting array unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
