GATE Computer ScienceVery hardGATE CS (PYQ pattern)

Two sorted lists of 8 keys each are merged by the standard merge, which stops comparing when one list runs out. All C(16, 8) = 12870 interleavings of the two lists are possible. For how many of them does the merge make exactly 15 comparisons?

  1. A. 6864
  2. B. 12870
  3. C. 3432
  4. D. 6435

Correct answer: A. 6864

The merge makes 16 − r comparisons, where r is the length of the final run of keys from the same list. 15 comparisons means r = 1: the last two keys come from different lists. Fixing the last two (AB or BA) leaves C(14, 7) = 3432 arrangements each, so 2·3432 = 6864.

More Algorithms questions

Exams that ask this topic