GATE / CS ARCHIVE

SIMILAR QUESTIONS / ALGORITHMS

Pair elements to find both minimum and maximum

3 questions solved the same way, newest first. Back to Algorithms

How these are solved

First compare elements in pairs, then find the maximum among winners and minimum among losers. The same comparison accounting yields 3n/2−2 for even n and ceil(3n/2)−2 generally.

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

2 marks · NAT
Report issue

Searching, Sorting & Selection

Question 39, GATE 2014: What is the minimum worst-case number of comparisons needed to find both the minimum and maximum of 100 numbers?
Tap to enlarge
Answer & solution

Official answer: 147.1 to 148.1

Our answer: 148 worst-case comparisons

1. Method and setup. Finding extrema separately takes 99+99 comparisons and wastes information. Pair the elements: one comparison per pair identifies a candidate for the minimum and a candidate for the maximum. Thereafter only the pair-losers need participate in the minimum search and only the pair-winners in the maximum search.

2. Work through the result and its assumptions. Compare50 disjoint pairs. Find the minimum among 50 smaller values in 49 comparisons and maximum among 50 larger values in 49. Total 50+49+49=148. This matches the optimal worst-case lower bound ceil(3n/2)−2; an adversary forces enough first comparisons to classify candidates before eliminating all but one in each class.

Original paper, page 17 · Answer key

2 marks · MCQ
Report issue

Comparison count to find both minimum and maximum

Searching, Sorting & Selection

Question 50, GATE 2007: Given an array of an even number n of values, both its minimum and maximum must be found. Which comparison-count claim is true?
Options:
(A) At least 2n−c comparisons are necessary for some constant c
(B) At most 1.5n−2 comparisons are sufficient
(C) At least n log₂ n comparisons are necessary
(D) None of these
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. Approach. A comparison within a pair simultaneously rules one element out as maximum and the other out as minimum. Exploit this dual use before separately reducing the two candidate sets.

2. Derivation and interpretation. Partition the even number n of values into n/2 pairs. Compare within each pair, putting its smaller member in a minimum-candidate set and larger member in a maximum-candidate set. Find the minimum of the n/2 smaller values and maximum of the n/2 larger values. The comparison count is n/2+(n/2−1)+(n/2−1)=3n/2−2, so B. This construction disproves the claimed asymptotically larger lower bounds in A and C.

Original paper, page 12