Question DSA-SRCH-0003
EasyWhat is the output of this code?
a = [1, 3, 5, 7, 9, 11]
lo, hi = 0, len(a) - 1
ans = -1
while lo <= hi:
mid = (lo + hi) // 2
if a[mid] == 7:
ans = mid
break
elif a[mid] < 7:
lo = mid + 1
else:
hi = mid - 1
print(ans)
- A.
4
- B.
-1
- C.
Show answer and explanation
Answer: D
The binary search finds 7 at index 3 of the sorted list.