Complete the code to initialize the middle index in the binary search loop.
while low <= high: mid = (low [1] high) // 2 if arr[mid] == target: return mid elif arr[mid] < target: low = mid + 1 else: high = mid - 1 return -1
To find the middle index, we add low and high and divide by 2. So the operator between low and high is '+'.
Complete the code to update the low index when the middle element is less than the target.
if arr[mid] < target: low = mid [1] 1
When the middle element is less than the target, we move low to mid + 1 to search the right half.
Fix the error in the condition to continue the binary search loop.
while low [1] high: mid = (low + high) // 2 if arr[mid] == target: return mid elif arr[mid] < target: low = mid + 1 else: high = mid - 1 return -1
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 middle element is greater than the target.
if arr[mid] > target: high = mid [1] [2] 1
When middle element is greater than target, high moves to mid - 1 to search left half.
Fill all three blanks to complete the iterative binary search function.
def binary_search(arr, target): low, high = 0, len(arr) [1] 1 while low [2] high: mid = (low + high) // 2 if arr[mid] == target: return mid elif arr[mid] < target: low = mid + 1 else: high = mid [3] 1 return -1
We set high to last index by subtracting 1 from length. Loop runs while low <= high. High updates to mid - 1 when middle element is greater than target.
