Complete the code to calculate the middle index in binary search.
int mid = low + (high - low) / [1];The middle index is found by dividing the range by 2.
Complete the code to check if the middle element is the target.
if (arr[mid] == [1]) { return mid; }
We compare the middle element with the target value we are searching for.
Fix the error in the condition to search the left half when target is smaller.
else if (arr[mid] [1] target) { high = mid - 1; }
If the middle element is greater than the target, search the left half by moving high.
Fill both blanks to search the right half when target is greater.
else { [1] = mid + 1; [2] = high; }
When target is greater, move low to mid + 1 and keep high unchanged.
Fill all three blanks to complete the binary search function.
int binarySearch(int arr[], int [1], int [2], int target) { while ([1] <= [2]) { int mid = [1] + ([2] - [1]) / 2; if (arr[mid] == target) return mid; else if (arr[mid] > target) [2] = mid - 1; else [1] = mid + 1; } return -1; }
The function uses low and high as the search boundaries and updates them correctly.
