GATE / CS ARCHIVE

GATE CSE 2021 SET 1 / QUESTION 2

GATE CSE 2021 Set 1 Q2

Algorithms · Searching, Sorting & Selection · Full GATE CSE 2021 Set 1 paper

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

Searching, Sorting & Selection

Question 2, GATE 2021: An array contains n integers. Let t be the optimal worst-case number of element comparisons needed to find both its minimum and maximum. Which printed bound is intended? A t>2n−2; B t>3⌈n/2⌉ and t≤2n−2; C t>n and t≤3⌈n/2⌉; D t>⌈log2 n⌉ and t≤n. The source gives no small-n restriction.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Our answer: C for n≥5; small-n premise omitted

The optimal worst-case comparison count for simultaneous minimum and maximum is ceil(3n/2)−2. Compare elements in pairs, then compare each pair’s smaller element only against the running minimum and its larger element only against the running maximum; for even n this costs 3n/2−2, and for odd n it costs 3(n−1)/2. The standard adversary lower bound matches these counts. Consequently n<t≤3ceil(n/2) for n≥5, giving intended C. Literal qualification: for n=3 or 4, t=n, and for n=1 or 2 it is smaller, so the strict inequality in C is not valid for every unspecified n.

For even n, first compare one pair to initialize min/max (1 comparison); each of the remaining (n−2)/2 pairs needs one internal comparison and two comparisons with min/max. Total 1+3(n−2)/2=3n/2−2. For odd n, initialize both extrema to the first value for free, then spend three comparisons for each of (n−1)/2 pairs. Pseudocode for each pair (a,b): compare a,b; let lo=min(a,b), hi=max(a,b); compare lo with running_min and hi with running_max. For the matching lower bound, each nonmaximum needs a loss certificate and each nonminimum a win certificate, totaling 2n−2 certificates. An adversary classifies elements as untouched, winner-only, loser-only or both, and consistently places winner-only elements above loser-only elements. Comparing two untouched elements can create two certificates, but such comparisons are disjoint and number at most floor(n/2). Other comparisons can be answered to create at most one new certificate. Therefore at least 2n−2−floor(n/2)=ceil(3n/2)−2 comparisons are needed. This proof concerns worst-case comparisons, not a particular presorted input.

Original paper, page 12 · Answer key