Question

An unordered list contains nn distinct elements. The number of comparisons to find an element in this list that is neither maximum nor minimum is

Choose one option
Official answer and solution
Official answer
D
From
Official GATE 2015 answer key

Solution

Consider selecting any three distinct elements and finding their median.

Let the list contain nn distinct elements. We need to find an element xx that is neither the maximum nor the minimum of the list.

Pick any 3 distinct elements from the list, say a,b,ca, b, c:

  1. Compare aa and bb (1 comparison).
  2. Compare the winner and loser with cc (at most 2 additional comparisons).

The median of {a,b,c}\{a, b, c\} is strictly greater than min⁡(a,b,c)\min(a, b, c) and strictly less than max⁡(a,b,c)\max(a, b, c):

  • Because it is strictly greater than at least one element in the list, it cannot be the global minimum.
  • Because it is strictly less than at least one element in the list, it cannot be the global maximum.

Hence, the median of the 3 chosen elements is guaranteed to be neither the global minimum nor the global maximum. The number of comparisons required is at most 3, which is independent of nn:

Θ(1).\Theta(1).
Final answer

D

Common trap

Attempting to find the global minimum and global maximum first (which would take Θ(n)\Theta(n) comparisons) instead of directly locating an intermediate element in Θ(1)\Theta(1).

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

Source

Paper
GATE 2015 Computer Science and Information Technology (CS) Session 2
Question
32 of 65, printed as CS Q.22
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.