There is a binary search algorithm, which, depending on the value of the last variable, returns the first or last entry of the element into the array
def binary_search(l, key, last): low = 0 high = len(l)-1 while low <= high: mid = (low + high) // 2 midVal = l[mid]; if (midVal < key or (last and midVal == key)): low = mid + 1 elif (midVal > key or ((not last) and midVal == key)): high = mid - 1 return high if last else low How to add a check for the existence of an element in the binary search algorithm?
if midval == key:- 0andriy