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): 80 48 51 45 64 6 5 92 4 10 97 13 58. What is the array after all insertions?

  1. A. 80 48 51 45 64 6 5 92 4 10 97 13 58
  2. B. 4 5 6 48 10 13 45 92 80 64 97 51 58
  3. C. 97 92 58 64 80 51 5 45 4 10 48 6 13
  4. D. 4 5 6 45 10 13 51 92 80 48 97 64 58

Correct answer: B. 4 5 6 48 10 13 45 92 80 64 97 51 58

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