def binary_search(array, x):
left: int = 0
right: int = len(array)
while left < right:
midian = (left + right) // 2
if array[midian] == x:
return True
elif array[midian] < x:
left = midian + 1
else:
right = midian
return False
if __name__ == "__main__":
array: int = [5, 7, 9, 12, 15, 18, 19, 20, 22]
x: int = 20
print(binary_search(array, x))
def max_to_border(array, x):
if not array or array[0] > x:
return -1
left: int = 0
right: int = len(array)
while left + 1 < right:
midian = (left + right) // 2
if array[midian] <= x:
left = midian
else:
right = midian
return array[left]
if __name__ == "__main__":
array: int = [5, 7, 9, 12, 15, 18, 19, 20, 22]
x: int = 14
print(max_to_border(array, x))