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): 92 88 28 91 87 25 8 61 24 5 20 73. What is the array after all insertions?

  1. A. 92 88 28 91 87 25 8 61 24 5 20 73
  2. B. 92 91 73 88 87 28 8 61 24 5 20 25
  3. C. 5 8 20 28 25 24 91 61 92 87 88 73
  4. D. 5 8 25 61 20 73 28 92 87 91 24 88

Correct answer: D. 5 8 25 61 20 73 28 92 87 91 24 88

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