GATE Computer ScienceVery hardGATE CS (PYQ pattern)
For n = 7 distinct keys, what is the maximum number of swaps that the bottom-up (Floyd) build-heap for a max-heap can perform, over all 5040 input orders?
- A. 5
- B. 6
- C. 7
- D. 4
Correct answer: D. 4
Indices 1 and 2 have only leaf children, so each can swap at most once; the root can sink two levels, at most 2 swaps: 1 + 1 + 2 = 4, attained for example by 1 2 3 4 5 6 7 (checked over all 5040 orders).
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- The stock span of day i is the number of consecutive days ending at i (including i) whose price is at most the price on day i. Prices are…
- The largest rectangle in a histogram with bar heights 2, 1, 5, 6, 2, 3, 4, 1, 3 (unit widths) is found with a stack of indices; a sentinel…
- void insert(Stack s, int x) { if (empty(s) || top(s) <= x) { push(s, x); return; } int y = pop(s); insert(s, x); push(s, y); } void…
- Kahn's topological sort uses a FIFO queue seeded with all vertices of in-degree 0 in increasing order; vertices are dequeued, and…
- An undirected graph has edges 1-2, 2-3, 3-4, 4-5, 5-6, 6-1, 2-7, 7-8, 8-9, 9-7, 4-10, 10-11. Vertices are coloured by the parity of their…
- int range(Node n, int lo, int hi) { if (!n) return 0; int c = (lo <= n->k && n->k <= hi); if (lo < n->k) c += range(n->l, lo, hi); if…