StreakPeaked· Practice

ExamsGATETechnical

An array of `n` numbers is given, where `n` is even. The maximum as well as the minimum of these `n` numbers needs to be determined. Which of the following is true about the number of comparisons needed?

  1. At least `2n − c` comparisons, for some constant `c`, are needed.
  2. At most `1.5n − 2` comparisons are needed.
  3. At least `n log₂ n` comparisons are needed.
  4. None of the above.

Correct answer: At most `1.5n − 2` comparisons are needed.

Solution

The minimum and maximum can be found efficiently by comparing elements in pairs. For even `n`, the optimal number of comparisons is `3n/2 - 2 = 1.5n - 2`. This is better than finding min and max separately.

Related GATE Technical questions

⚔️ Practice GATE Technical free + battle 1v1 →