Question
An unordered list contains distinct elements. The number of comparisons to find an element in this list that is neither maximum nor minimum is
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 distinct elements. We need to find an element that is neither the maximum nor the minimum of the list.
Pick any 3 distinct elements from the list, say :
- Compare and (1 comparison).
- Compare the winner and loser with (at most 2 additional comparisons).
The median of is strictly greater than and strictly less than :
- 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 :
D
Attempting to find the global minimum and global maximum first (which would take comparisons) instead of directly locating an intermediate element in .
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.