Skip to content
Official PYQ · 2007

GATE CS 2007 Q50 — Asymptotic Worst Case Time and Space Complexity

Explain with AI

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

Choose one option
View worked solution

Answer: B

To find both the maximum and minimum elements in an array of nn numbers (where nn is even), we can use an optimal pairwise comparison algorithm to minimize the number of comparisons:

  1. Initialize: Compare the first two elements of the array. Assign the larger element to max and the smaller element to min.
    This requires 11 comparison.
  2. Process Remaining Elements: Group the remaining n−2n - 2 elements into pairs. There will be n−22\frac{n - 2}{2} pairs. For each pair:
    • Compare the two elements of the pair with each other to find the local maximum and local minimum (11 comparison).
    • Compare the local maximum with the global max (11 comparison).
    • Compare the local minimum with the global min (11 comparison).
    Thus, each of the n−22\frac{n - 2}{2} pairs requires exactly 33 comparisons.

Summing these up, the total number of comparisons is:

Total Comparisons=1+3×(n−22)=1+1.5n−3=1.5n−2\text{Total Comparisons} = 1 + 3 \times \left(\frac{n - 2}{2}\right) = 1 + 1.5n - 3 = 1.5n - 2

Since we can achieve this with exactly 1.5n−21.5n - 2 comparisons, it is true that at most 1.5n−21.5n - 2 comparisons are needed.

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

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