Skip to content
Official PYQ · 2015

GATE CS 2015 Set 2 Q22 — Asymptotic Worst Case Time and Space Complexity

Explain with AI

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
View worked solution

Answer: D

To find an element in an unordered list of nn distinct elements (n≥3n \ge 3) that is neither the maximum nor the minimum, we can use a constant-time approach:

  1. Pick any three arbitrary elements from the list, for example, the first three elements: aa, bb, and cc.
  2. Find the median of these three elements.

Why does this work?

  • Let the median of the three elements be mm. By definition of a median of three distinct elements, one element is strictly smaller than mm and one element is strictly larger than mm.
  • Since there is at least one element in the list smaller than mm, mm cannot be the global minimum.
  • Since there is at least one element in the list larger than mm, mm cannot be the global maximum.

Comparison Complexity:

Finding the median of three elements requires at most 33 comparisons. Since this number of comparisons is independent of nn, the time complexity is Θ(1)\Theta(1).

Therefore, the correct option is D.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Asymptotic Worst Case Time and Space Complexity questions