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)?
- A. 210
- B. 372
- C. 330
- 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.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- A singly linked list 10 -> 11 -> ... -> 19 is rearranged by a routine that places all nodes at odd positions (1st, 3rd, ...) first…
- A circular singly linked list holds nodes 1 to 100 in order. Deletion starts with node 1 itself and then removes every second remaining…
- struct T { int k; struct T l, r; }; int f(struct T n) { return n ? 1 + max(f(n->l), f(n->r)) : 0; } int g(struct T n) { return n ?…
- Keys 30, 10, 40, 5, 20, 35, 50, 15, 25, 45 are inserted in this order into an empty BST, and then the left and right pointers of every…
- A complete binary tree with 20 nodes is stored in A[1..20] with A[i] = i, the children of i at 2i and 2i + 1. The routine v(i) does: if i…
- int a[4][3] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; int (p)[3] = a; p += 2; int q = p + 1; printf("%d %d %d %d", ((p - 1) + 2), (p)[1]…