Binary Search Recursive Approach in DSA C++ - Time & Space Complexity
We want to understand how the time taken by recursive binary search changes as the input size grows.
Specifically, how many steps does it take to find an element in a sorted list?
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;
if (arr[mid] > target) return binarySearch(arr, left, mid - 1, target);
return binarySearch(arr, mid + 1, right, target);
}
This code searches for a target value in a sorted array by repeatedly dividing the search range in half.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive call that halves the search range each time.
- How many times: At most, the function calls itself until the search range is empty, roughly halving the size each time.
Each step cuts the search space in half, so the number of steps grows slowly as input size increases.
| 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 number of steps grows slowly, increasing by one each time the input size doubles.
[X] Wrong: "Binary search checks every element one by one, so it is O(n)."
[OK] Correct: Binary search does not check all elements; it cuts the search space in half each step, so it is much faster than checking one by one.
Understanding binary search time complexity shows you can analyze efficient algorithms and explain why they work well on large data.
"What if we changed the recursive binary search to an iterative version? How would the time complexity change?"
Practice
binary search recursive approach?Solution
Step 1: Understand binary search logic
Binary search works by dividing the array into two halves and deciding which half to search next based on the middle element.Step 2: Apply recursion in binary search
Recursion calls the same function on the half where the target might be, reducing the search space each time.Final Answer:
Divide the array into halves and search recursively in the relevant half -> Option BQuick Check:
Binary search = divide and conquer [OK]
- Searching all elements recursively instead of half
- Sorting inside the search function
- Not reducing search space correctly
Solution
Step 1: Identify when search should stop
The search should stop when the start index is greater than the end index, meaning the element is not found.Step 2: Confirm base case condition
Checking if start > end returns -1 to indicate the target is missing.Final Answer:
if (start > end) return -1; -> Option AQuick Check:
Base case = start > end means not found [OK]
- Using start == end as base case incorrectly
- Returning wrong index when not found
- Confusing start and end conditions
int binarySearch(int arr[], int start, int end, int target) {
if (start > end) return -1;
int mid = start + (end - start) / 2;
if (arr[mid] == target) return mid;
else if (arr[mid] > target) return binarySearch(arr, start, mid - 1, target);
else return binarySearch(arr, mid + 1, end, target);
}
int main() {
int arr[] = {2, 4, 6, 8, 10};
int index = binarySearch(arr, 0, 4, 6);
std::cout << index;
return 0;
}Solution
Step 1: Identify the target and array
The array is {2,4,6,8,10} and target is 6.Step 2: Trace recursive calls
First mid = 2 (index 2), arr[2] = 6 matches target, so return 2 immediately.Final Answer:
2 -> Option AQuick Check:
Target 6 found at index 2 [OK]
- Confusing index with value
- Off-by-one errors in mid calculation
- Not returning correct index
int binarySearch(int arr[], int start, int end, int target) {
if (start >= end) return -1;
int mid = (start + end) / 2;
if (arr[mid] == target) return mid;
else if (arr[mid] > target) return binarySearch(arr, start, mid - 1, target);
else return binarySearch(arr, mid + 1, end, target);
}Solution
Step 1: Analyze base case condition
The base case incorrectly uses start >= end, which misses checking when start == end.Step 2: Correct base case for full search
It should be start > end to allow checking the last element at start == end.Final Answer:
Base case condition should be start > end, not start >= end -> Option CQuick Check:
Base case = start > end to stop [OK]
- Using >= instead of > in base case
- Wrong mid calculation formula
- Returning wrong values
Solution
Step 1: Understand duplicates in sorted array
Duplicates mean target can appear multiple times; we want the first index.Step 2: Modify recursive search on match
When arr[mid] == target, recursively search left half to check for earlier occurrence, else return mid.Step 3: Implement logic
If left recursive call returns valid index, return it; otherwise return mid.Final Answer:
When arr[mid] == target, continue searching left half to find earlier occurrence -> Option DQuick Check:
Find first occurrence = search left on match [OK]
- Returning first found index without checking left side
- Searching right half on match
- Using linear search unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
