Discussion
Minimum number of comparisons required to compute the largest and second largest element in array is
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.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development