🧠
Binary Search (Recursive)
💡 This approach uses recursion to implement binary search, which can be more intuitive for some but risks stack overflow on large inputs.
Intuition
Recursively narrow down the search space by comparing the middle element to the target and searching the appropriate half.
Algorithm
- Define a recursive function with low and high indices.
- If low is greater than high, return -1 (target not found).
- Calculate mid as the average of low and high.
- If nums[mid] equals target, return mid.
- If nums[mid] is less than target, recurse on right half (mid + 1 to high).
- Else, recurse on left half (low to mid - 1).
💡 The recursion must have a clear base case to avoid infinite calls and stack overflow.
def binary_search_recursive(nums, target, low, high):
if low > high:
return -1
mid = low + (high - low) // 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
return binary_search_recursive(nums, target, mid + 1, high)
else:
return binary_search_recursive(nums, target, low, mid - 1)
# Driver code
if __name__ == '__main__':
nums = [-1,0,3,5,9,12]
target = 9
print(binary_search_recursive(nums, target, 0, len(nums) - 1)) # Output: 4
Line Notes
if low > highBase case: no valid search space left
mid = low + (high - low) // 2Calculate middle index safely
if nums[mid] == targetCheck if mid element is target
return binary_search_recursive(nums, target, mid + 1, high)Recurse on right half if target is greater
public class BinarySearchRecursive {
public static int binarySearchRecursive(int[] nums, int target, int low, int high) {
if (low > high) {
return -1;
}
int mid = low + (high - low) / 2;
if (nums[mid] == target) {
return mid;
} else if (nums[mid] < target) {
return binarySearchRecursive(nums, target, mid + 1, high);
} else {
return binarySearchRecursive(nums, target, low, mid - 1);
}
}
public static void main(String[] args) {
int[] nums = {-1,0,3,5,9,12};
int target = 9;
System.out.println(binarySearchRecursive(nums, target, 0, nums.length - 1)); // Output: 4
}
}
Line Notes
if (low > high)Base case: search space invalid
int mid = low + (high - low) / 2Calculate mid index safely
if (nums[mid] == target)Check if mid element matches target
return binarySearchRecursive(nums, target, mid + 1, high)Recurse on right half if target is greater
#include <iostream>
#include <vector>
using namespace std;
int binarySearchRecursive(const vector<int>& nums, int target, int low, int high) {
if (low > high) {
return -1;
}
int mid = low + (high - low) / 2;
if (nums[mid] == target) {
return mid;
} else if (nums[mid] < target) {
return binarySearchRecursive(nums, target, mid + 1, high);
} else {
return binarySearchRecursive(nums, target, low, mid - 1);
}
}
int main() {
vector<int> nums = {-1,0,3,5,9,12};
int target = 9;
cout << binarySearchRecursive(nums, target, 0, nums.size() - 1) << endl; // Output: 4
return 0;
}
Line Notes
if (low > high)Base case: no valid search space
int mid = low + (high - low) / 2Calculate mid index safely
if (nums[mid] == target)Check if mid element equals target
return binarySearchRecursive(nums, target, mid + 1, high)Recurse on right half if target is greater
function binarySearchRecursive(nums, target, low, high) {
if (low > high) {
return -1;
}
let mid = low + Math.floor((high - low) / 2);
if (nums[mid] === target) {
return mid;
} else if (nums[mid] < target) {
return binarySearchRecursive(nums, target, mid + 1, high);
} else {
return binarySearchRecursive(nums, target, low, mid - 1);
}
}
// Driver code
const nums = [-1,0,3,5,9,12];
const target = 9;
console.log(binarySearchRecursive(nums, target, 0, nums.length - 1)); // Output: 4
Line Notes
if (low > high)Base case to stop recursion when search space is empty
let mid = low + Math.floor((high - low) / 2)Calculate mid index safely
if (nums[mid] === target)Check if mid element matches target
return binarySearchRecursive(nums, target, mid + 1, high)Recurse on right half if target is greater
TimeO(log n)
SpaceO(log n)
Time complexity is logarithmic due to halving search space each call. Space complexity is logarithmic due to recursion call stack.
💡 For large n, recursion depth can be about 20 calls, which uses more memory than iterative approach.
Interview Verdict: Accepted but less space efficient than iterative
Recursive binary search is elegant but can cause stack overflow on very large inputs; iterative is preferred in interviews.