GATE Computer ScienceHardGATE CS (PYQ pattern)

The array [5, 3, 17, 10, 84, 19, 6, 22, 9, 15, 2] is turned into a max-heap by Floyd's linear-time Build-Max-Heap. Which array results? (Inserting the elements one by one into an empty heap would give a different array.)

  1. A. [84, 22, 19, 17, 15, 5, 6, 3, 9, 10, 2]
  2. B. [84, 22, 19, 10, 15, 17, 6, 5, 9, 3, 2]
  3. C. [84, 22, 19, 10, 15, 3, 6, 5, 9, 17, 2]
  4. D. [84, 22, 17, 19, 15, 10, 6, 5, 9, 3, 2]

Correct answer: B. [84, 22, 19, 10, 15, 17, 6, 5, 9, 3, 2]

Sift down from node 5 to node 1 (1-indexed); the root 5 ends up deep after 84 and 22 rise, and the run (checked by simulation) gives [84, 22, 19, 10, 15, 17, 6, 5, 9, 3, 2]. The first distractor is what repeated insertion produces.

More Algorithms questions

Exams that ask this topic