Data Structures & AlgorithmsVery hardGATE CS · Product placement

An array holds n distinct elements. What is the minimum number of comparisons that ANY comparison-based algorithm needs, in the worst case, to find the second-largest element?

  1. A. 2n − 3
  2. B. n + ⌈log₂ n⌉ − 2
  3. C. ⌈3n/2⌉ − 2
  4. D. n − 1

Correct answer: B. n + ⌈log₂ n⌉ − 2

A knockout tournament finds the maximum in n − 1 comparisons; the second largest lost directly to the maximum, and there are at most ⌈log₂ n⌉ such candidates, needing ⌈log₂ n⌉ − 1 more. ⌈3n/2⌉ − 2 is the bound for finding both max and min.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic