Question
Let be a binary min-heap consisting of elements implemented as an array. What is the worst case time complexity of an optimal algorithm to find the maximum element in ?
Official answer and solution
- Official answer
- C
- From
- Official GATE 2021 answer key
Solution
Analyze the structure of a binary min-heap to find where the maximum element can reside.
In a binary min-heap, the value at any node is less than or equal to the values of its children. By induction, the maximum element cannot be an internal node that has children, because each child would be greater than or equal to it.
Therefore, the maximum element must be located at one of the leaf nodes.
In an -element binary heap represented as an array, the leaves occupy indices from to . The number of leaves is .
The leaf elements are not stored in sorted order. Finding the maximum among them requires scanning all leaves. Thus, the worst-case time complexity of an optimal algorithm to find the maximum is .
C
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2021 Computer Science and Information Technology - Session 2 (CS2)
- Question
- 12 of 65, printed as CS Q.2
- 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.