Skip to content
Official PYQ · 2021

GATE CS 2021 Set 2 Q2 — Binary Heaps

Explain with AI

​​​​​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
View worked solution

Answer: C

In a binary min-heap of size nn, the elements satisfy the min-heap property where the key of each node is less than or equal to the keys of its children. Consequently:

  • The minimum element is always located at the root node (index 00 or 11 in the array representation).
  • The maximum element cannot have any children, meaning it must be located at one of the leaf nodes.

For a complete binary tree with nn elements, the leaf nodes are located in the second half of the array, specifically from index ⌊n/2⌋\lfloor n/2 \rfloor to n−1n-1 (using 0-based indexing). The number of leaf nodes is ⌈n/2⌉\lceil n/2 \rceil.

Since there is no relative ordering or heap property maintained among the leaf nodes themselves, finding the maximum element requires a linear scan over all the leaf nodes.

The time complexity to find the maximum among the ⌈n/2⌉\lceil n/2 \rceil unsorted leaf elements is:

T(n)=Θ(n2)=Θ(n)T(n) = \Theta\left(\frac{n}{2}\right) = \Theta(n)

Therefore, the worst-case time complexity of an optimal algorithm to find the maximum element in a binary min-heap is Θ(n)\Theta(n).

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

Published
Report a correction
Keep exploringMore Binary Heaps questions