Data Structures & AlgorithmsVery hardGATE CS

Two sorted lists A = [2, 4, 4, 7, 9, 12, 15, 18, 21] and B = [1, 4, 5, 6, 13, 14, 16, 17] are merged with the standard merge procedure (one comparison A[i] <= B[j] per output element while both lists are non-empty, then the rest of the remaining list is copied without comparisons). How many comparisons are made?

  1. A. 15
  2. B. 16
  3. C. 13
  4. D. 17

Correct answer: A. 15

Comparisons stop when one list is exhausted; here that happens after 15 comparisons (the tail is copied without comparing). The general worst case m + n - 1 = 16 is not reached.

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

Exams that ask this topic