ProgrammingVery hardCognizant GenC · TCS NQT

What is printed?

What is printed?
def find_min(a):
    lo, hi = 0, len(a) - 1
    while lo < hi:
        mid = (lo + hi) // 2
        if a[mid] > a[hi]:
            lo = mid + 1
        else:
            hi = mid
    return a[lo], lo
print(find_min([4, 5, 6, 7, 0, 1, 2]), find_min([3, 1, 2]))
  1. A. (0, 5) (1, 1)
  2. B. (4, 4) (1, 1)
  3. C. (0, 4) (1, 1)
  4. D. (0, 4) (2, 2)

Correct answer: C. (0, 4) (1, 1)

Binary search compares the middle with the right end, which shows which half holds the rotation point. The minimum 0 sits at index 4, and in [3, 1, 2] the minimum 1 sits at index 1.

More Python Programming questions

Exams that ask this topic