Complete the code to initialize the middle index in the binary search loop.
int mid = (low [1] high) / 2;
The middle index is calculated by adding low and high, then dividing by 2.
Complete the code to update the low index when the target is greater than the middle element.
if (arr[mid] < target) { low = [1]; }
When the target is greater than the middle element, we move low to mid + 1 to search the right half.
Fix the error in the loop condition to correctly run the binary search.
while (low [1] high) { int mid = (low + high) / 2; // rest of code }
The loop should continue while low is less than or equal to high to cover all elements.
Fill both blanks to correctly update the high index when the target is less than the middle element.
if (target [1] arr[mid]) { high = [2]; }
If the target is less than the middle element, we move high to mid - 1 to search the left half.
Fill all three blanks to complete the binary search function that returns the index or -1 if not found.
int binarySearch(int arr[], int size, int target) {
int low = 0, high = size [1] 1;
while (low [2] high) {
int mid = (low + high) / 2;
if (arr[mid] == target) return [3];
else if (arr[mid] < target) low = mid + 1;
else high = mid - 1;
}
return -1;
}We set high to size - 1 because array indices start at 0. The loop runs while low is less than or equal to high. When found, return mid index.