Find Second largest number in array at most nlog₂n−2 comparisons
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Finding the second largest number in an array is a common problem that is often covered in computer science courses. The straightforward approach requires comparisons when scanning through the array twice or sorting the array first and then selecting the second largest element. However, a more efficient method can achieve this task using at most comparisons, which leverages tournament-based selection. This solution is optimal for this problem and highlights the interplay between data structure traversal and comparison minimization.
The Problem
Given an array of distinct numbers, our objective is to find the second largest number with the fewest comparisons. The challenge is to cleverly organize the comparisons to improve efficiency beyond the brute-force methods.
Methodology
The method discussed utilizes a concept where an array is visualized as participants in a tournament. Through this perspective:
- Initial Tournament (Finding The Largest): • Compare elements in the array in pairs. • The larger number from each pair progresses to the next round. • Continue pairing and comparing until a single winner (the largest number in the array) is found.The maximum number of comparisons at this stage is because each element competes and only the largest moves forward.
- Finding The Second Largest: • After identifying the largest number, the second-largest number must be among those that lost to it. • Compare the largest number with the set of numbers it defeated, identifying which is the largest of the losers.Since the largest number defeats a number in every comparison it is involved, it should have been directly compared to competitors (in the case of a power of two, others adjust similarly but ensure comparisons for each match tree level).
Using this method, the largest number is found in comparisons, and the second-largest is found in comparisons among the defeated competitors. Thus, the total number of comparisons is .
Example
Consider an array `[5, 8, 3, 2, 7, 9, 6, 1]`. Here's how the process unfolds:
- Tournament for Largest Element: • Round 1: `[5 vs 8, 3 vs 2, 7 vs 9, 6 vs 1]` -> `[8, 3, 9, 6]` • Round 2: `[8 vs 3, 9 vs 6]` -> `[8, 9]` • Round 3: `[8 vs 9]` -> `[9]`Here, comparisons are needed to find the largest number "9".
- Finding the Second Largest: • Only elements that lost to "9" can be the second largest. The elements are `{7, 6}`. • Comparison: `7 vs 6` -> `7` is the second largest among the defeated.
In total, comparisons were used, consistent with the theoretical maximum of .
Summary Table
| Step | Number of Comparisons | Description |
| Initial Tournament | Identify the largest number through pairwise elimination. | |
| Second Largest Selection | Compare largest's competitors to find the largest loser. | |
| Total | Optimal number of comparisons required for the process. |
Additional Details
Complexity
• Time Complexity: The operations are completed in time. The tournament selection ( rounds) ensures an efficient comparison strategy.
• Space Complexity: The space complexity is , primarily for storing the defeated numbers of the largest value to determine the second largest.
Edge Cases
• Non-distinct Elements: This method assumes distinct elements as duplicates may alter the basic logic. • Non-power of Two Array Lengths: Arrays not of size require careful handling of asymmetric brackets but maintain the same order of operations.
Real-World Applications
Such efficient approaches are beneficial in: • Competitive programming, where time is limited. • Algorithms requiring frequent second-largest evaluations, such as streaming data analysis.
In conclusion, through an intelligent application of divide and conquer alongside a structured comparison hierarchy, determining the second largest number in an array becomes optimal and efficient. This knowledge not only leads to better algorithms but also deepens comprehension of fundamental computer science principles.
Related reading
- Find set of numbers in one collection that adds up to a number in another
- Find Shortest Binary String In Given Interval
- Find shortest subarray containing all elements
- Find simplest regular expression matching all given strings
- Find the 2nd largest element in an array with minimum number of comparisons
- Find the best combination from a given set of multiple sets
- find smallest area that contains all the rectangles
- Find the centroid of a polygon with weighted vertices

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.