GATE / CS ARCHIVE

SIMILAR QUESTIONS / DATA STRUCTURES

Minimize swaps restoring max-heap order

2 questions solved the same way, newest first. Back to Data Structures

How these are solved

Locate parent-child violations, establish a lower bound on changed positions, and exhibit arbitrary pair swaps satisfying every heap inequality. Both optimize arbitrary swaps, not bottom-up heapify cost.

1 mark · MCQ
Report issue

Asked again GATE 1996, Q2.11 (different numbers)

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

2 marks · MCQ
Report issue

Asked again GATE 2015 Set 3, Q30 (different numbers)

Fewest swaps to make a max-heap

Heaps & Priority Queues

Question 2.11, GATE 1996: For array [89,19,40,17,12,10,2,5,7,11,6,9,70], find the minimum arbitrary pair swaps to obtain a max-heap.
Tap to enlarge
Your answer
Answer & solution

Answer: C: 2 (no official key was published)

Using one-based indices, swap entries 13 and 6, then 6 and 3. The array becomes [89,19,70,17,12,40,2,5,7,11,6,9,10], and every parent is at least as large as its children. For a lower bound, initially 70 at index 13 exceeds parent 10 at index 6. Any one-swap repair must change one of those positions. Moving a sufficiently large value into position 6 either puts 70 below a smaller parent or leaves position 6 larger than its parent 40; moving 70 directly to position 3 leaves 40 at 13 above parent 10. Exhaustive checking of all 78 pairs also finds no one-swap heap. Thus two are necessary and sufficient.

Original paper, page 5