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): 39 25 53 76 40 67 65 37 26 3 24. What is the array after all insertions?

  1. A. 3 24 53 37 25 67 65 76 39 40 26
  2. B. 39 25 53 76 40 67 65 37 26 3 24
  3. C. 76 53 67 37 40 39 65 25 26 3 24
  4. D. 3 24 25 39 37 26 76 65 53 40 67

Correct answer: A. 3 24 53 37 25 67 65 76 39 40 26

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