An unordered list contains distinct elements. The number of comparisons to find an element in this list that is neither maximum nor minimum is
View worked solution
Answer: D
To find an element in an unordered list of distinct elements () that is neither the maximum nor the minimum, we can use a constant-time approach:
- Pick any three arbitrary elements from the list, for example, the first three elements: , , and .
- Find the median of these three elements.
Why does this work?
- Let the median of the three elements be . By definition of a median of three distinct elements, one element is strictly smaller than and one element is strictly larger than .
- Since there is at least one element in the list smaller than , cannot be the global minimum.
- Since there is at least one element in the list larger than , cannot be the global maximum.
Comparison Complexity:
Finding the median of three elements requires at most comparisons. Since this number of comparisons is independent of , the time complexity is .
Therefore, the correct option is D.
Free practice · Your answers on this page are not saved.
