GATE / CS ARCHIVE

GATE CSE 2020 / QUESTION 47

GATE CSE 2020 Q47

Data Structures · Heaps & Priority Queues · Full GATE CSE 2020 paper

GATE 2020, Q47 2 marks · NAT
Report issue

Heaps & Priority Queues

Question 47, GATE 2020: An array-represented binary min-heap contains 1023 elements. What is the minimum guaranteed number of comparisons needed to find a maximum?
Tap to enlarge
Answer & solution

Official answer: 511

Worked explanation

In a one-based heap array, node i has children 2i and 2i+1 when present. Therefore internal nodes are 1…floor(1023/2)=511 and leaves are 512…1023, totaling 512. Every internal value is at most a descendant leaf value by repeatedly applying the min-heap property, so a maximum exists among leaves. There is no ordering constraint between distinct leaves. In the worst case each of 511 nonwinning leaf candidates must lose a comparison to eliminate it; one comparison can eliminate at most one candidate. A scan maintaining the largest leaf seen uses exactly 511 comparisons and attains that bound.

Derivation and answer

The1023-node complete binary min-heap has 512 leaves, at array positions 512–1023 under one-based indexing. A maximum can be chosen among the leaves; their relative values are otherwise unconstrained by the heap property. Finding the maximum of 512 candidates requires 511 comparisons in the worst case, achievable by a linear scan. This is the optimal guaranteed comparison count, not a claim about every favorable tied instance.

Original paper, page 14 · Answer key