Data Structures & AlgorithmsHardGATE CS · DSA

A sorted array was rotated to give [35, 45, 52, 68, 78, 1, 5, 12, 18, 27, 28]. Binary search finds the index of the minimum: while lo < hi, mid = (lo + hi) // 2; if a[mid] > a[hi] then lo = mid + 1 else hi = mid. What index is returned and how many iterations run?

  1. A. index 5, 3 iterations
  2. B. index 4, 3 iterations
  3. C. index 5, 4 iterations
  4. D. index 6, 3 iterations

Correct answer: A. index 5, 3 iterations

Comparing with a[hi] tells which half is sorted; the minimum 1 is at index 5 (the rotation point), reached after 3 iterations.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic