GATE CS 2024 Set 1 — Question 41

MCQ+2 / -0.67MediumHeapify & Build-HeapHeapsProgramming & Data Structures

Programming & Data Structures → Heaps → Heapify & Build-Heap

Last updated

Question

An array [82,101,90,11,111,75,33,131,44,93][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?
A.
82, 90, 101
B.
82, 11, 93
C.
131, 11, 93
D.
131, 111, 90

Correct answer

(D) 131, 111, 90

Solution

The problem asks for the first three elements of the array after it has been heapified. Since the options suggest the root is 131 (the maximum element in the input), we assume a Max-Heap is constructed using the standard bottom-up build-heap algorithm.
Input Array: A=[82,101,90,11,111,75,33,131,44,93]A = [82, 101, 90, 11, 111, 75, 33, 131, 44, 93]
Indices: 00 to 99. Size n=10n=10.
The build-heap process starts from the last non-leaf node, which is at index n21=1021=4\lfloor \frac{n}{2} \rfloor - 1 = \lfloor \frac{10}{2} \rfloor - 1 = 4, and iterates down to 00.
1.Index 4 (A[4]=111A[4]=111): Child is A[9]=93A[9]=93. Since 111>93111 > 93, no swap needed.
Array: [82,101,90,11,111,75,33,131,44,93][82, 101, 90, 11, \mathbf{111}, 75, 33, 131, 44, 93]
2.Index 3 (A[3]=11A[3]=11): Children are A[7]=131A[7]=131 and A[8]=44A[8]=44. Max child is 131131. Swap 1111 and 131131.
Array: [82,101,90,131,111,75,33,11,44,93][82, 101, 90, \mathbf{131}, 111, 75, 33, \mathbf{11}, 44, 93]
3.Index 2 (A[2]=90A[2]=90): Children are A[5]=75A[5]=75 and A[6]=33A[6]=33. 9090 is larger than both. No swap.
Array: [82,101,90,131,111,75,33,11,44,93][82, 101, \mathbf{90}, 131, 111, 75, 33, 11, 44, 93]
4.Index 1 (A[1]=101A[1]=101): Children are A[3]=131A[3]=131 and A[4]=111A[4]=111. Max child is 131131. Swap 101101 and 131131.
Array: [82,131,90,101,111,75,33,11,44,93][82, \mathbf{131}, 90, \mathbf{101}, 111, 75, 33, 11, 44, 93]
Check subtree at index 3: Parent 101101, Children 11,4411, 44. 101101 is larger. No further swap.
5.Index 0 (A[0]=82A[0]=82): Children are A[1]=131A[1]=131 and A[2]=90A[2]=90. Max child is 131131. Swap 8282 and 131131.
Array: [131,82,90,101,111,75,33,11,44,93][\mathbf{131}, \mathbf{82}, 90, 101, 111, 75, 33, 11, 44, 93]
Check subtree at index 1: Parent 8282, Children A[3]=101,A[4]=111A[3]=101, A[4]=111. Max child is 111111. Swap 8282 and 111111.
Array: [131,111,90,101,82,75,33,11,44,93][131, \mathbf{111}, 90, 101, \mathbf{82}, 75, 33, 11, 44, 93]
Check subtree at index 4: Parent 8282, Child A[9]=93A[9]=93. Max child is 9393. Swap 8282 and 9393.
Array: [131,111,90,101,93,75,33,11,44,82][131, 111, 90, 101, \mathbf{93}, 75, 33, 11, 44, \mathbf{82}]Final Heapified Array: [131,111,90,101,93,75,33,11,44,82][131, 111, 90, 101, 93, 75, 33, 11, 44, 82]The first three elements are 131, 111, 90.

More questions on Heaps

Practice GATE CS PYQs with adaptive difficulty

Timed practice, skill tracking, and AI explanations — free to start.

Start practicing free