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): 36 80 75 55 64 23 26 82 62 59 38. What is the array after all insertions?
- A. 36 80 75 55 64 23 26 82 62 59 38
- B. 82 80 75 64 59 23 26 36 62 55 38
- C. 23 38 26 62 55 75 36 82 80 64 59
- D. 23 26 38 36 59 55 75 82 80 62 64
Correct answer: C. 23 38 26 62 55 75 36 82 80 64 59
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 (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…
- 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…