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): 90 94 43 18 86 29 81 60 50 99 61 91 98. What is the array after all insertions?
- A. 90 94 43 18 86 29 81 60 50 99 61 91 98
- B. 18 43 29 50 61 90 81 94 60 99 86 91 98
- C. 99 94 98 60 90 91 43 18 50 86 61 29 81
- D. 18 29 43 60 50 61 90 81 94 99 86 91 98
Correct answer: B. 18 43 29 50 61 90 81 94 60 99 86 91 98
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 (children of index i at 2i+1 and…
- Distinct integers are inserted in this order into an empty binary min-heap stored in a 0-indexed array (children of index i at 2i+1 and…
- Distinct integers are inserted in this order into an empty binary min-heap stored in a 0-indexed array (children of index i at 2i+1 and…
- Distinct integers are inserted in this order into an empty binary min-heap stored in a 0-indexed array (children of index i at 2i+1 and…