Complete the code to initialize the middle index in the binary search loop.
mid := low [1] (high - low) / 2
The middle index is found by adding low to half the distance between high and low, so use '+'.
Complete the code to update the low index when the target is greater than the middle element.
if arr[mid] < target { low = [1] }
When target is greater, move low to mid + 1 to search the right half.
Fix the error in the loop condition to correctly run binary search while low is less than or equal to high.
for low [1] high { // binary search steps }
The loop must run while low is less than or equal to high to include all elements.
Fill both blanks to update the high index when the target is less than the middle element.
if target < arr[mid] { high = [1] } else { low = [2] }
If target is less, move high to mid - 1; else move low to mid + 1.
Fill all three blanks to complete the binary search function that returns the index of target or -1 if not found.
func binarySearch(arr []int, target int) int {
low, high := 0, len(arr) - 1
for low [1] high {
mid := low + (high - low) / 2
if arr[mid] == target {
return [2]
} else if arr[mid] < target {
low = [3]
} else {
high = mid - 1
}
}
return -1
}The loop runs while low <= high; return mid when found; update low to mid + 1 if target is greater.