GATE / CS ARCHIVE

GATE CSE 2015 SET 3 / QUESTION 30

GATE CSE 2015 Set 3 Q30

Data Structures · Heaps & Priority Queues · Full GATE CSE 2015 Set 3 paper

GATE 2015 Set 3, Q30 1 mark · MCQ
Report issue

Heaps & Priority Queues

Question 30, GATE 2015: An array in level-order heap representation is (89,19,50,17,12,15,2,5,7,11,6,9,100). What is the minimum number of interchanges needed to convert it into a max-heap? An interchange swaps two elements. Options: (A) 4; (B) 5; (C) 2; (D) 3.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

1. With 1-based indices, swaps (13,6), (6,3), (3,1) yield (100,19,89,17,12,50,2,5,7,11,6,9,15). Check each parent against its children: every parent is at least as large. This gives a concrete three-swap witness, not merely the count performed by an unspecified heapify routine.

2. Three swaps along the ancestor chain move 100 through indices 13→6→3→1, producing a valid max-heap. Fewer cannot suffice: 100 must reach the root. If swapped directly with 89, the resulting 89 at 13 exceeds both ancestors 15 at 6 and 50 at 3; one additional swap cannot repair both (swapping with 3 leaves 50 below 15; swapping with 6 leaves 89 below 50). If100 reaches the root in two swaps, at least one of the remaining displaced chain values is still out of heap order. Thus the minimum is 3, D.

Original paper, page 63 · Answer key