Data Structures & AlgorithmsVery hard

When computing the k-th smallest element of two sorted arrays, why can a binary search on the answer's position replace a full merge?

  1. A. The arrays can be indexed in random order only.
  2. B. Comparing the k/2-th elements of both arrays discards half of a candidate range each step, giving O(log k) work.
  3. C. k must be a power of two for the method to work.
  4. D. The answer is always found in the first array.

Correct answer: B. Comparing the k/2-th elements of both arrays discards half of a candidate range each step, giving O(log k) work.

Comparing the midpoints of the two candidate prefixes lets one whole prefix be discarded safely, halving the search for the k-th element each step.

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

Exams that ask this topic