Search in Rotated Sorted Array in DSA C# - Time & Space Complexity
We want to understand how fast we can find a number in a rotated sorted array.
The question is: how does the search time grow as the array gets bigger?
Analyze the time complexity of the following code snippet.
public int Search(int[] nums, int target) {
int left = 0, right = nums.Length - 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: The loop runs until the search range is empty, 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.
Time Complexity: O(log n)
This means the search time grows slowly, even if the array becomes very large.
[X] Wrong: "Because the array is rotated, we must check every element, so it takes linear time."
[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 many interviews look for.
"What if the array was not rotated but still sorted? How would the time complexity change?"
Practice
Solution
Step 1: Understand the array structure
A rotated sorted array is created by taking a sorted array and rotating it at some pivot. This splits it into two sorted parts.Step 2: Why normal binary search fails
Normal binary search assumes the entire array is sorted. Here, because of rotation, one half is sorted and the other may not be, so direct binary search won't work.Final Answer:
The array is split into two sorted parts, making normal binary search invalid directly -> Option CQuick Check:
Rotation splits array = challenge for binary search [OK]
- Assuming the whole array is sorted
- Ignoring the pivot point
- Confusing rotation with reversal
Solution
Step 1: Understand array indexing in C#
Arrays are zero-indexed, so the first element is at 0 and the last at Length - 1.Step 2: Correct initialization for binary search
Binary search starts with left at 0 and right at last index (Length - 1). This covers the entire array.Final Answer:
int left = 0, right = nums.Length - 1; -> Option DQuick Check:
Zero-based indexing means right = Length - 1 [OK]
- Setting right to Length instead of Length - 1
- Starting left at 1 instead of 0
- Using negative indices
int[] nums = {4,5,6,7,0,1,2};?
int left = 0, right = nums.Length - 1;
int target = 3;
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;Solution
Step 1: Understand the array and target
The array is rotated: [4,5,6,7,0,1,2]. Target is 3, which is not in the array.Step 2: Trace the binary search steps
Binary search checks mid and adjusts left/right. Since 3 is not present, loop ends and returns -1.Final Answer:
-1 -> Option AQuick Check:
Target missing returns -1 [OK]
- Assuming target 3 is in array
- Returning wrong index for missing target
- Not updating left/right correctly
int left = 0, right = nums.Length - 1;
while (left < right) {
int mid = (left + right) / 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;Solution
Step 1: Analyze loop condition
The loop uses left < right, which misses the case when left == right, possibly skipping the last element check.Step 2: Correct loop condition for binary search
Binary search requires left <= right to ensure all elements are checked, including when left equals right.Final Answer:
The loop condition should be left <= right, not left < right -> Option AQuick Check:
Use left <= right for complete search [OK]
- Using < instead of <= in loop
- Incorrect mid calculation causing overflow
- Misplaced return statements
int[] nums = {15, 18, 2, 3, 6, 12};, write a C# method to find the index of target 3 using binary search in rotated array. What is the returned index?Solution
Step 1: Understand the array and target
The array is rotated: [15, 18, 2, 3, 6, 12]. Target is 3, which is present at index 3.Step 2: Apply binary search in rotated array
Using the binary search logic for rotated arrays, the search will find target 3 at index 3.Final Answer:
3 -> Option BQuick Check:
Target 3 found at index 3 [OK]
- Confusing index of 3 with nearby elements
- Not handling rotation correctly
- Returning -1 for existing target
Ready to prove it?
Pass the test and this pattern moves unknown
