Data Structures & AlgorithmsHardGATE 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): 67 65 43 3 30 85 69 50 89 29. What is the array after all insertions?

  1. A. 67 65 43 3 30 85 69 50 89 29
  2. B. 3 29 65 50 30 85 69 67 89 43
  3. C. 89 85 69 65 30 43 67 3 50 29
  4. D. 3 29 30 50 43 85 69 65 89 67

Correct answer: B. 3 29 65 50 30 85 69 67 89 43

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