Machine-translated by Google. Questions are authored in English.
Top

Discussion

Minimum number of comparisons required to compute the largest and second largest element in array is

  • A.n − ⌈log2n⌉ − 2
  • B.n + ⌈log2n⌉ − 2
  • C.log2n
  • D.None of these

Answer: B

Correct answer: n + ⌈log2n⌉ − 2

Run a knockout tournament. Finding the largest takes \(n - 1\) comparisons, and the second largest must be one of the elements that lost directly to the winner - there are \(\lceil \log_2 n \rceil\) of them, since that is the depth of the tournament. Finding the best of those costs \(\lceil \log_2 n \rceil - 1\) more:

\[ (n - 1) + \lceil \log_2 n \rceil - 1 = n + \lceil \log_2 n \rceil - 2 \]

The naive approach - two separate passes - would cost about \(2n\), so the tournament saves nearly half.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.