Binary Search as Divide and Conquer in DSA C - Time & Space Complexity
We want to understand how fast binary search finds an item in a sorted list.
How does the number of steps change as the list gets bigger?
Analyze the time complexity of the following code snippet.
int binarySearch(int arr[], int left, int right, int target) {
if (left > right) return -1;
int mid = left + (right - left) / 2;
if (arr[mid] == target) return mid;
else if (arr[mid] > target) return binarySearch(arr, left, mid - 1, target);
else return binarySearch(arr, mid + 1, right, target);
}
This code searches for a target number by repeatedly dividing the list in half.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking the middle element and deciding which half to search next.
- How many times: The search space halves each time, so this happens about log₂(n) times.
Each step cuts the list size 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 list size adds only one extra step.
Time Complexity: O(log n)
This means the number of steps grows slowly, making binary search very efficient for large lists.
[X] Wrong: "Binary search checks every element like a simple search."
[OK] Correct: Binary search skips half the list each time, so it does far fewer checks than searching one by one.
Understanding binary search time helps you explain efficient searching clearly and confidently in interviews.
"What if the list was not sorted? How would the time complexity of searching change?"
Practice
Binary Search as a divide and conquer algorithm?Solution
Step 1: Understand divide and conquer in binary search
Binary search splits the array into two halves and decides which half to search next.Step 2: Identify the efficiency of halving the search space
By halving the search space each time, it quickly narrows down the target location.Final Answer:
It divides the search range in half each time to find the target efficiently. -> Option BQuick Check:
Divide and conquer = halving search space [OK]
- Thinking binary search checks every element
- Confusing binary search with sorting
- Believing binary search sums elements
Solution
Step 1: Understand safe middle index calculation
Usinglow + (high - low) / 2prevents integer overflow whenlowandhighare large.Step 2: Compare with other options
int mid = (low + high) / 2; can overflow iflow + highexceeds integer limit; options B and C are incorrect formulas.Final Answer:
int mid = low + (high - low) / 2; -> Option CQuick Check:
Safe mid calculation = low + (high - low)/2 [OK]
- Using (low + high)/2 causing overflow
- Subtracting incorrectly in mid calculation
- Multiplying low and high instead of adding
int arr[] = {2, 4, 6, 8, 10};
int low = 0, high = 4, target = 6;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target) {
printf("%d", mid);
break;
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}Solution
Step 1: Trace the binary search steps
Initial low=0, high=4, mid=2. arr[2]=6 matches target 6.Step 2: Print the mid index
Since arr[mid] == target, the code prints 2 and breaks the loop.Final Answer:
2 -> Option AQuick Check:
Target found at index 2 [OK]
- Not updating low and high correctly
- Confusing index with value
- Off-by-one errors in mid calculation
int binarySearch(int arr[], int low, int high, int target) {
while (low < high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target) return mid;
else if (arr[mid] < target) low = mid + 1;
else high = mid - 1;
}
return -1;
}Solution
Step 1: Analyze the loop condition
The loop useswhile (low < high), which misses checking when low equals high.Step 2: Correct the loop condition
Changing towhile (low <= high)ensures the last element is checked.Final Answer:
Change while condition to low <= high -> Option AQuick Check:
Loop must include low == high to check all elements [OK]
- Using < instead of <= in loop condition
- Incorrect mid calculation causing overflow
- Returning wrong value when target not found
Solution
Step 1: Understand duplicates in binary search
Standard binary search returns any occurrence, not necessarily the first.Step 2: Modify search to find first occurrence
After finding target at mid, continue searching left half to check for earlier occurrences.Final Answer:
After finding target, continue searching left half to find earlier occurrence. -> Option DQuick Check:
Find first occurrence by searching left after match [OK]
- Stopping at first found index ignoring duplicates
- Searching right half instead of left after match
- Switching to linear search unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
