Skip to content
Official PYQ · 2020

GATE CS 2020 Q47 — Binary Heaps

Explain with AI
Consider the array representation of a binary min-heap containing 10231023 elements. The minimum number of comparisons required to find the maximum in the heap is ___________.
View worked solution

Answer: 511

In a binary min-heap, the parent node is always smaller than or equal to its children. Consequently, the maximum element cannot be an internal node (as any internal node is smaller than its descendants). Thus, the maximum element must reside in one of the leaf nodes.

For a binary heap represented as an array of size NN (using 1-based indexing):

  • The internal nodes are located at indices 11 to ⌊N/2⌋\lfloor N/2 \rfloor.
  • The leaf nodes are located at indices from ⌊N/2⌋+1\lfloor N/2 \rfloor + 1 to NN.

Given N=1023N = 1023:

First leaf index=⌊1023/2⌋+1=511+1=512\text{First leaf index} = \lfloor 1023/2 \rfloor + 1 = 511 + 1 = 512

Thus, the leaf nodes are located at indices from 512512 to 10231023.

The total number of leaf nodes (LL) is:

L=1023−512+1=512L = 1023 - 512 + 1 = 512

Since there is no ordering guarantee among the leaf nodes, finding the maximum element among these LL elements requires finding the maximum of an unsorted list of size LL.

The minimum number of comparisons required to find the maximum of LL unsorted elements is:

L−1=512−1=511L - 1 = 512 - 1 = 511

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

Published
Report a correction
Keep exploringMore Binary Heaps questions