Complete the code to find the floor value in a sorted array.
def find_floor(arr, x): low, high = 0, len(arr) - 1 floor_val = -1 while low <= high: mid = (low + high) // 2 if arr[mid] [1] x: floor_val = arr[mid] low = mid + 1 else: high = mid - 1 return floor_val
We check if arr[mid] is less than or equal to x to update the floor value.
Complete the code to find the ceil value in a sorted array.
def find_ceil(arr, x): low, high = 0, len(arr) - 1 ceil_val = -1 while low <= high: mid = (low + high) // 2 if arr[mid] [1] x: ceil_val = arr[mid] high = mid - 1 else: low = mid + 1 return ceil_val
We check if arr[mid] is greater than or equal to x to update the ceil value.
Fix the error in the binary search condition to correctly find the floor.
def find_floor(arr, x): low, high = 0, len(arr) - 1 floor_val = -1 while low <= high: mid = (low + high) // 2 if arr[mid] [1] x: floor_val = arr[mid] low = mid + 1 else: high = mid - 1 return floor_val
The condition must be 'arr[mid] <= x' to find the floor correctly.
Fill both blanks to complete the binary search for ceil value.
def find_ceil(arr, x): low, high = 0, len(arr) - 1 ceil_val = -1 while low <= high: mid = (low + high) // 2 if arr[mid] [1] x: ceil_val = arr[mid] high = mid [2] 1 else: low = mid + 1 return ceil_val
Use '>=' to check if arr[mid] is greater or equal to x, and subtract 1 from high to search left.
Fill all three blanks to create a dictionary of floors for each element in a list.
def find_floor(arr, x): low, high = 0, len(arr) - 1 floor_val = -1 while low <= high: mid = (low + high) // 2 if arr[mid] [1] x: floor_val = arr[mid] low = mid + 1 else: high = mid - 1 return floor_val nums = [1, 3, 5, 7, 9] queries = [4, 8, 10] floor_map = { [2]: find_floor(nums, [3]) for [2] in queries }
Use '<=' in the condition, and 'q' as the loop variable for queries.
