Skip to content
Official PYQ · 2024

GATE CS 2024 Set 1 Q31 — Searching, Sorting, Hashing

Explain with AI

​​​​​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?

Choose one option
View worked solution

Answer: D

Since the options start with either 82 or 131 (the maximum element of the array), and no option starts with 11 (the minimum element), the question refers to a max-heap.

We apply the standard bottom-up Build-Max-Heap algorithm on the 1-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.

The non-leaf nodes are at indices ⌊n/2⌋\lfloor n/2 \rfloor down to 11, which are indices 5,4,3,2,15, 4, 3, 2, 1.

  • Heapify at index 5 (111111): Children are at index 10 (9393). Since 111>93111 > 93, no swap is needed.
    Array: [82,101,90,11,111,75,33,131,44,93][82, 101, 90, 11, 111, 75, 33, 131, 44, 93]
  • Heapify at index 4 (1111): Children are at indices 8 (131131) and 9 (4444). Swap 1111 with the maximum child 131131.
    Array: [82,101,90,131,111,75,33,11,44,93][82, 101, 90, 131, 111, 75, 33, 11, 44, 93]
  • Heapify at index 3 (9090): Children are at indices 6 (7575) and 7 (3333). Since 90>7590 > 75 and 90>3390 > 33, no swap is needed.
    Array: [82,101,90,131,111,75,33,11,44,93][82, 101, 90, 131, 111, 75, 33, 11, 44, 93]
  • Heapify at index 2 (101101): Children are at indices 4 (131131) and 5 (111111). Swap 101101 with the maximum child 131131.
    Array: [82,131,90,101,111,75,33,11,44,93][82, 131, 90, 101, 111, 75, 33, 11, 44, 93]
  • Heapify at index 1 (8282): Children are at indices 2 (131131) and 3 (9090). Swap 8282 with 131131.
    Array: [131,82,90,101,111,75,33,11,44,93][131, 82, 90, 101, 111, 75, 33, 11, 44, 93]
    Now, heapify the swapped element 8282 at index 2. Its children are at indices 4 (101101) and 5 (111111). Swap 8282 with 111111.
    Array: [131,111,90,101,82,75,33,11,44,93][131, 111, 90, 101, 82, 75, 33, 11, 44, 93]
    Now, heapify 8282 at index 5. Its child is at index 10 (9393). Swap 8282 with 9393.
    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 of the heapified array are 131,111,90131, 111, 90.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Searching, Sorting, Hashing questions