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: heap[p]≤heap[2p]\text{heap}[p] \le \text{heap}[2p] and heap[p]≤heap[2p+1]\text{heap}[p] \le \text{heap}[2p + 1].
  • 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 ≥\ge 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 N=1023N = 1023:
    • Number of internal nodes = ⌊N/2⌋=⌊1023/2⌋=511\lfloor N / 2 \rfloor = \lfloor 1023 / 2 \rfloor = 511 (indices 1 through 511 in 1-based indexing).
    • Number of leaf nodes = N−⌊N/2⌋=1023−511=512N - \lfloor N / 2 \rfloor = 1023 - 511 = 512 (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 L=512L = 512 completely unordered elements, any comparison-based algorithm must perform at least L−1L - 1 comparisons in the worst case:
    Comparisons=512−1=511\text{Comparisons} = 512 - 1 = 511
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.