Question

The minimum number of comparisons required to find the minimum and the maximum of 100 numbers is _________________.

Official answer and solution
Accepted answers
147.1 to 148.1
From
Official GATE 2014 answer key

Solution

Use the optimal pairwise comparison method to find both minimum and maximum of an array simultaneously.

For finding both the minimum and maximum of nn numbers, the standard divide-and-conquer / pairwise comparison algorithm proceeds as follows:

  1. Pair up the n=100n = 100 elements into n/2=50n / 2 = 50 pairs.
  2. Compare elements in each pair: this requires n2=50\frac{n}{2} = 50 comparisons.
    • The 50 winners are candidates for the maximum.
    • The 50 losers are candidates for the minimum.
  3. Find the maximum among the 50 maximum candidates: this requires 50−1=4950 - 1 = 49 comparisons.
  4. Find the minimum among the 50 minimum candidates: this requires 50−1=4950 - 1 = 49 comparisons.

Total number of comparisons:

Total=50+49+49=148\text{Total} = 50 + 49 + 49 = 148

In general, for even nn, the optimal number of comparisons is:

3n2−2=3(100)2−2=150−2=148\frac{3n}{2} - 2 = \frac{3(100)}{2} - 2 = 150 - 2 = 148
Final answer

148

Common trap

Finding minimum first in n−1=99n-1 = 99 comparisons and maximum separately in n−2=98n-2 = 98 comparisons, totaling 197197 comparisons, which is not minimal.

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2014 Computer Science and Information Technology (CS) Session 1
Question
49 of 65, printed as CS Q.39
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.