Skip to content
Official PYQ · 2021

GATE CS 2021 Set 1 Q2 — Arrays, Stacks and Queues

Explain with AI

​​​​​Let PP be an array containing nn integers. Let tt be the lowest upper bound on the number of comparisons of the array elements, required to find the minimum and maximum values in an arbitrary array of nn elements. Which one of the following choices is correct?

Choose one option
View worked solution

Answer: C

To find both the minimum and maximum elements in an arbitrary array of size nn, we can use an optimal pairwise comparison algorithm:

  1. If nn is odd, initialize both min and max to the first element. If nn is even, compare the first two elements, set min to the smaller one and max to the larger one.
  2. Process the remaining elements in pairs. For each pair, compare the two elements with each other, then compare the smaller element with min and the larger element with max.

The total number of comparisons, tt, required by this optimal algorithm is:

  • If nn is even: 3n2−2 comparisons\frac{3n}{2} - 2 \text{ comparisons} (1 comparison for the first pair, and 3×(n−22)3 \times \left(\frac{n-2}{2}\right) comparisons for the remaining pairs).
  • If nn is odd: 3(n−1)2 comparisons\frac{3(n-1)}{2} \text{ comparisons} (3×n−123 \times \frac{n-1}{2} comparisons for the pairs).

We can write this compactly as:

t=⌈3n2⌉−2(for n≥2)t = \left\lceil \frac{3n}{2} \right\rceil - 2 \quad (\text{for } n \ge 2)

Let us analyze the bounds for tt when n≥5n \ge 5:

  • Since t≈1.5n−2t \approx 1.5n - 2, we have t>nt > n for all n≥5n \ge 5.
  • Comparing with 3⌈n2⌉3\lceil \frac{n}{2}\rceil:
    • For even nn: t=1.5n−2<1.5n=3⌈n2⌉t = 1.5n - 2 < 1.5n = 3\lceil \frac{n}{2}\rceil.
    • For odd nn: t=1.5(n−1)<1.5(n+1)=3⌈n2⌉t = 1.5(n-1) < 1.5(n+1) = 3\lceil \frac{n}{2}\rceil.
    Thus, t≤3⌈n2⌉t \le 3\lceil \frac{n}{2}\rceil holds true.

Therefore, the correct choice is C: t>n and t≤3⌈n2⌉t > n \text{ and } t \le 3\lceil \frac{n}{2}\rceil.

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

Published
Report a correction
Keep exploringMore Arrays, Stacks and Queues questions