GATE / CS ARCHIVE

GATE CSE 1996 / QUESTION 2.11

Fewest swaps to make a max-heap

Data Structures · Heaps & Priority Queues · Full GATE CSE 1996 paper

GATE 1996, Q2.11 2 marks · MCQ
Report issue

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