An array is heapified. Which one of the following options represents the first three elements in the heapified array?
GATE CSE 2024Algorithms › Sorting AlgorithmsVerified
GATE CSE 2024 PYQ: An array [ 82 , 101 , 90 , 11 , 111 , 75 , 33 , 131 , 44 , 93 ] [82,101,90,11,111,75,33,131,44,93] [ 82…
- A82,90,101
- B82,11,93
- C131,11,93
- D131,111,90Correct answer
Answer
(D) 131 , 111 , 90 131,111,90 131 , 111 , 90
Approach
Treat the array as a complete binary tree and build a max-heap using bottom-up heapify, starting from the last internal node. The required first three elements are the first three positions of the final heap array.
Building a max-heap from the given array places the largest element 131 at the root, then heapifies the second position to 111 and the third position remains 90. Thus the first three elements are 131,111,90.
Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.