GATE / CS ARCHIVE

GATE CSE 2021 SET 2 / QUESTION 2

GATE CSE 2021 Set 2 Q2

Data Structures · Heaps & Priority Queues · Full GATE CSE 2021 Set 2 paper

GATE 2021 Set 2, Q2 1 mark · MCQ
Report issue

Heaps & Priority Queues

Question 2, GATE 2021: A binary min-heap of n elements is stored as an array. Find optimal worst-case time to find its maximum. A Θ(1); B Θ(log n); C Θ(n); D Θ(n log n).
Tap to enlarge
Your answer
Answer & solution

Official answer: C

A maximum occurs among the heap leaves, of which there are ceil(n/2). Scan those leaves to find it in O(n). Their values have no heap-imposed relative order, so in the worst case each must be considered, yielding Ω(n). Hence Θ(n), C.

In a one-based heap array, indices floor(n/2)+1 through n are leaves. Initialize best to the first leaf and compare each remaining leaf once. Heap order says ancestors are no larger than descendants, so inspecting internal nodes is unnecessary. An adversary can assign distinct arbitrary values to leaves above all internal values; finding their maximum requires leaf-count−1 comparisons.

Original paper, page 55 · Answer key