GATE Computer ScienceVery hardGATE CS (PYQ pattern)

Two sorted lists, X with 5 keys and Y with 7 keys (all 12 keys distinct), are merged with the standard two-pointer merge that stops comparing as soon as one list is exhausted. Over all C(12,5) = 792 possible relative orders of the keys, for how many does the merge make exactly 11 comparisons (the maximum)?

  1. A. 210
  2. B. 372
  3. C. 330
  4. D. 420

Correct answer: D. 420

Comparisons = 12 - (length of the run left over when one list empties), and that run is the final block of the merged order coming from a single list. The maximum 11 needs that block to have length 1, i.e. the last two keys of the merged order come from different lists. Ending ...XY: arrange the other 4 X and 6 Y freely, C(10,4) = 210; ending ...YX: C(10,6) = 210. Total 420.

More Programming and Data Structures (GATE) questions

Exams that ask this topic