Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div2 B

Distinct integers are inserted in this order into an empty binary min-heap stored in a 0-indexed array (the children of index i are at 2i+1 and 2i+2, and a new element is sifted up while it is smaller than its parent): 74 87 23 10 59 41 7 22 49 91 94 60 84. What is the array after all insertions?

  1. A. 74 87 23 10 59 41 7 22 49 91 94 60 84
  2. B. 94 91 84 49 87 60 7 10 22 59 74 23 41
  3. C. 7 10 41 22 49 59 74 23 87 91 94 60 84
  4. D. 7 22 10 23 59 60 41 87 49 91 94 74 84

Correct answer: D. 7 22 10 23 59 60 41 87 49 91 94 74 84

Every insertion appends to the end and swaps upward past larger parents; the parent of index i is (i-1)//2. Skipping the sift-up, building a max-heap, or using i//2 as the parent gives the trap arrays.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic