Question DSA-SRCH-0061
HardWhat is the output of this code?
def search(a, t):
lo, hi = 0, len(a) - 1
while lo <= hi:
m = (lo + hi) // 2
if a[m] == t: return m
if a[lo] <= a[m]:
if a[lo] <= t < a[m]: hi = m - 1
else: lo = m + 1
else:
if a[m] < t <= a[hi]: lo = m + 1
else: hi = m - 1
return -1
a = [15, 18, 22, 3, 6, 9, 12]
print(search(a, 6), search(a, 18), search(a, 10))
- A.
-1 1 -1
- B.
4 1 6
- C.
Sign up free to see the answer and a step-by-step explanation.