Question
An array [82, 101, 90, 11, 111, 75, 33, 131, 44, 93] is heapified. Which one of the following options represents the first three elements in the heapified array?
Official answer and solution
- Official answer
- D
- From
- Official GATE 2024 answer key
Solution
Run the standard bottom-up Build-Max-Heap algorithm from the last internal node down to the root.
Given the 0-indexed array of size , we apply bottom-up max-heapify starting from index down to :
- (
111): child at is93no change. - (
11): children at are131,44swap11with131: . - (
90): children at are75,33no change. - (
101): children at are131,111swap101with131(); at ,101stops. - (
82): children at are131,90swap82with131(); at , children at are101,111swap82with111(); at , child at is93swap82with93().
The resulting max-heap array is [131, 111, 90, 101, 93, 75, 33, 11, 44, 82], whose first three elements are 131, 111, 90.
Option (D)
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2024 Computer Science and Information Technology - Set 1 (CS1)
- Question
- 41 of 65
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.