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 time taken 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 value by repeatedly dividing the search range in half.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive call cutting the search range in half.
- How many times: About half the size of the range each time, repeating until one element remains.
Each step cuts the list size roughly 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 input size adds only one extra step.
Time Complexity: O(log n)
This means the time grows slowly, increasing by one step each time the input size doubles.
[X] Wrong: "Binary search checks every element one by one like linear search."
[OK] Correct: Binary search skips half the elements each step, so it does far fewer checks than linear search.
Understanding binary search time helps you explain efficient searching and shows you can analyze divide and conquer methods.
"What if the array 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 continue searching based on the middle element.Step 2: Identify the main action in binary search
It discards the half where the target cannot be, reducing the search space quickly.Final Answer:
It divides the search space into two halves and discards one half each time. -> Option AQuick Check:
Divide and conquer = split and discard half [OK]
- Thinking binary search checks every element
- Confusing binary search with linear search
- Assuming binary search sorts the array
Solution
Step 1: Understand why to avoid overflow
Using (low + high) / 2 can cause overflow if low and high are large integers.Step 2: Use safe middle calculation
Calculating mid as low + (high - low) / 2 avoids overflow by subtracting before adding.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
- Using subtraction incorrectly in mid calculation
- Multiplying low and high instead of adding
int binarySearch(int arr[], int low, int high, int x) {
if (low > high) return -1;
int mid = low + (high - low) / 2;
if (arr[mid] == x) return mid;
else if (arr[mid] > x) return binarySearch(arr, low, mid - 1, x);
else return binarySearch(arr, mid + 1, high, x);
}
int main() {
int arr[] = {2, 4, 6, 8, 10};
int n = 5;
int result = binarySearch(arr, 0, n - 1, 6);
std::cout << result;
return 0;
}Solution
Step 1: Trace the binary search calls
Initial call: low=0, high=4, mid=2, arr[2]=6 matches target 6, so return 2.Step 2: Return the index found
Since arr[2] == 6, the function returns 2 immediately.Final Answer:
2 -> Option DQuick Check:
Index of 6 in array = 2 [OK]
- Confusing index with value
- Not updating mid correctly
- Returning -1 when element is present
int binarySearch(int arr[], int low, int high, int x) {
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == x) return mid;
else if (arr[mid] < x) low = mid;
else high = mid - 1;
}
return -1;
}Solution
Step 1: Analyze the update of low pointer
When arr[mid] < x, low should move to mid + 1 to avoid infinite loop.Step 2: Identify the infinite loop cause
Setting low = mid keeps low same if mid equals low, causing infinite loop.Final Answer:
The update low = mid should be low = mid + 1. -> Option AQuick Check:
Increment low past mid to avoid infinite loop [OK]
- Not incrementing low causing infinite loop
- Using wrong loop condition
- Ignoring overflow in mid calculation
{1, 2, 2, 2, 3, 4}. How can you modify binary search to find the first occurrence of 2?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
When arr[mid] == target, continue searching left half to find earlier occurrence.Step 3: Stop when mid is first or left element is different
Return mid if it is first element or previous element is different.Final Answer:
Keep searching left half even after finding 2 to find first occurrence. -> Option BQuick Check:
Find first occurrence by searching left after match [OK]
- Returning first found index without checking left
- Searching right half after finding target
- Using linear search unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
