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): 41 57 44 88 89 10 42 31 46. What is the array after all insertions?
- A. 41 57 44 88 89 10 42 31 46
- B. 89 88 44 46 57 10 42 31 41
- C. 10 31 44 41 46 57 88 42 89
- D. 10 31 41 46 89 44 42 88 57
Correct answer: D. 10 31 41 46 89 44 42 88 57
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.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- 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…
- 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…
- 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…
- 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…
- 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…
- 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…