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 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
Indices: to . Size .The
Check subtree at index 3: Parent , Children . is larger. No further swap.
Check subtree at index 1: Parent , Children . Max child is . Swap and .
Array:
Check subtree at index 4: Parent , Child . Max child is . Swap and .
Array: Final Heapified Array: The first three elements are 131, 111, 90.
build-heap algorithm.Input Array: Indices: to . Size .The
build-heap process starts from the last non-leaf node, which is at index , and iterates down to .1.Index 4 (): Child is . Since , no swap needed.
Array: 2.Index 3 (): Children are and . Max child is . Swap and .
Array: 3.Index 2 (): Children are and . is larger than both. No swap.
Array: 4.Index 1 (): Children are and . Max child is . Swap and .
Array: Check subtree at index 3: Parent , Children . is larger. No further swap.
5.Index 0 (): Children are and . Max child is . Swap and .
Array: Check subtree at index 1: Parent , Children . Max child is . Swap and .
Array:
Check subtree at index 4: Parent , Child . Max child is . Swap and .
Array: Final Heapified Array: The first three elements are 131, 111, 90.
More questions on Heaps
2024 Set 2 Q13Consider the following C program. Assume parameters to a function are evaluated from right to left.…2024 Set 2 Q17Let be the adjacency matrix of a simple undirected graph . Suppose is its own inverse.…2024 Set 1 Q17Given an integer array of size , we want to check if the array is sorted (in either ascending or…2024 Set 1 Q18Consider the following C program: [code] Which one of the following statements is CORRECT?2024 Set 1 Q19Consider the following C program: [code] Assume that the input to the program from the command line…
Practice GATE CS PYQs with adaptive difficulty
Timed practice, skill tracking, and AI explanations — free to start.
Start practicing free