Data Structures & AlgorithmsVery hard

Why does the median of two sorted arrays of sizes m and n not require merging both arrays fully?

  1. A. The median is always the first element of the larger array.
  2. B. The median of two sorted arrays equals the average of their first elements.
  3. C. A binary search over the partition point of one array determines the median in O(log(min(m, n))) time.
  4. D. Median computation is impossible without merging.

Correct answer: C. A binary search over the partition point of one array determines the median in O(log(min(m, n))) time.

Choosing how many elements come from each array fixes the left half; a binary search on the split of the smaller array finds the correct split quickly. Only the boundary elements are then needed.

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

Exams that ask this topic