Data Structures & AlgorithmsHardGATE CS · Codeforces Div2 C

A 0-indexed array holds these values: 42 43 87 63 97 68 6 3 74 71. 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. 97 74 87 63 71 68 6 3 42 43
  2. B. 63 43 87 42 97 68 6 3 74 71
  3. C. 87 97 68 74 71 42 6 3 63 43
  4. D. 97 87 68 74 71 43 6 3 42 63

Correct answer: A. 97 74 87 63 71 68 6 3 42 43

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