Data Structures & AlgorithmsHardGATE CS · Codeforces Div2 C

A 0-indexed array holds these values: 10 74 2 30 69 14 16 86 49. A max-heap is built in place with Floyd's method: for i from n/2 − 1 down to 0, sift the element at index i down (swap with the larger child while it is smaller than that child). What is the array after building?

  1. A. 86 74 14 30 69 2 16 10 49
  2. B. 74 69 16 86 10 14 2 30 49
  3. C. 86 74 16 69 30 2 14 10 49
  4. D. 86 74 16 49 69 14 2 30 10

Correct answer: D. 86 74 16 49 69 14 2 30 10

Floyd's method sifts down from the last internal node to the root, so each subtree is already a heap when its root is processed. Always choosing the left child, going top-down, or inserting one by one give other arrays.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic