Question

Let HH be a binary min-heap consisting of nn elements implemented as an array. What is the worst case time complexity of an optimal algorithm to find the maximum element in HH?

Choose one option
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 nn-element binary heap represented as an array, the leaves occupy indices from ⌊n/2⌋+1\lfloor n/2 \rfloor + 1 to nn. The number of leaves is ⌈n/2⌉=Θ(n)\lceil n/2 \rceil = \Theta(n).

The leaf elements are not stored in sorted order. Finding the maximum among them requires scanning all Θ(n)\Theta(n) leaves. Thus, the worst-case time complexity of an optimal algorithm to find the maximum is Θ(n)\Theta(n).

Final answer

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.