Question
Consider the array representation of a binary min-heap containing 1023 elements. The minimum number of comparisons required to find the maximum in the heap is _______.
Official answer and solution
- Official answer
- 511
- From
- Official GATE 2020 answer key
Solution
Identify the candidate positions for the maximum element in a min-heap and calculate comparisons among them.
Properties of a binary min-heap:
- In a min-heap, every parent node has a key less than or equal to the keys of its children: and .
- As a consequence, no internal node can hold the maximum key in the heap, because each internal node has at least one child with a key itself.
- Therefore, the maximum element must reside in one of the leaf nodes.
Count the leaf nodes:
- For an array-based complete binary tree of size :
- Number of internal nodes = (indices 1 through 511 in 1-based indexing).
- Number of leaf nodes = (indices 512 through 1023).
Finding the maximum among the leaves:
- The min-heap property enforces no ordering relationship among the leaf nodes.
- To find the maximum among completely unordered elements, any comparison-based algorithm must perform at least comparisons in the worst case:
Final answer
511
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2020 Computer Science and Information Technology (CS)
- Question
- 57 of 65, printed as CS Q.47
- 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.