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?

Choose one option
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 A=[82,101,90,11,111,75,33,131,44,93]A = [82, 101, 90, 11, 111, 75, 33, 131, 44, 93] of size n=10n = 10, we apply bottom-up max-heapify starting from index ⌊n/2⌋−1=4\lfloor n/2 \rfloor - 1 = 4 down to 00:

  • i=4i = 4 (111): child at 99 is 93 <111< 111 →\rightarrow no change.
  • i=3i = 3 (11): children at 7,87, 8 are 131, 44 →\rightarrow swap 11 with 131: A[3]=131,A[7]=11A[3] = 131, A[7] = 11.
  • i=2i = 2 (90): children at 5,65, 6 are 75, 33 <90< 90 →\rightarrow no change.
  • i=1i = 1 (101): children at 3,43, 4 are 131, 111 →\rightarrow swap 101 with 131 (A[1]=131,A[3]=101A[1] = 131, A[3] = 101); at i=3i = 3, 101 >max⁡(11,44)> \max(11, 44) →\rightarrow stops.
  • i=0i = 0 (82): children at 1,21, 2 are 131, 90 →\rightarrow swap 82 with 131 (A[0]=131,A[1]=82A[0] = 131, A[1] = 82); at i=1i = 1, children at 3,43, 4 are 101, 111 →\rightarrow swap 82 with 111 (A[1]=111,A[4]=82A[1] = 111, A[4] = 82); at i=4i = 4, child at 99 is 93 →\rightarrow swap 82 with 93 (A[4]=93,A[9]=82A[4] = 93, A[9] = 82).

The resulting max-heap array is [131, 111, 90, 101, 93, 75, 33, 11, 44, 82], whose first three elements are 131, 111, 90.

Final answer

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.