Complete the code to calculate the middle index in the binary search.
int mid = low + ([1] - low) / 2;
The middle index is calculated using the high and low indices to avoid overflow.
Complete the code to return the index when the target is found.
if (arr[mid] == target) return [1];
Return the middle index where the target is found.
Fix the error in the recursive call for searching the left half.
return binarySearch(arr, low, [1] - 1, target);
To search the left half, we reduce the high index to mid - 1.
Fill both blanks to complete the recursive call for searching the right half.
return binarySearch(arr, [1] + 1, [2], target);
To search the right half, start from mid + 1 to high index.
Fill all three blanks to complete the base case and recursive calls of binary search.
if (low > high) return [1]; int mid = low + (high - low) / 2; if (arr[mid] == target) return [2]; else if (arr[mid] > target) return binarySearch(arr, low, [3] - 1, target); else return binarySearch(arr, mid + 1, high, target);
Return -1 if not found, return mid if found, and use mid - 1 to search left half.
