Minimum Average of Smallest and Largest Elements - Problem
Imagine you're organizing a tournament elimination system! You have an array nums of n integers where n is even. Your task is to repeatedly pair up the smallest and largest remaining elements, calculate their average, and find the minimum average among all pairs.
The Process:
- Start with an empty
averagesarray - Repeat
n/2times:- Find and remove the smallest element (minElement)
- Find and remove the largest element (maxElement)
- Calculate
(minElement + maxElement) / 2.0and add to averages
- Return the minimum value in the averages array
Goal: Find the smallest possible average when pairing extremes optimally.
Input & Output
example_1.py — Basic Case
$
Input:
nums = [7,8,3,4,15,13,4,1]
›
Output:
5.5
💡 Note:
After sorting: [1,3,4,4,7,8,13,15]. Pairs: (1,15)→avg=8.0, (3,13)→avg=8.0, (4,8)→avg=6.0, (4,7)→avg=5.5. Minimum is 5.5.
example_2.py — Small Array
$
Input:
nums = [1,9,8,3,10,5]
›
Output:
5.5
💡 Note:
After sorting: [1,3,5,8,9,10]. Pairs: (1,10)→avg=5.5, (3,9)→avg=6.0, (5,8)→avg=6.5. Minimum is 5.5.
example_3.py — Two Elements
$
Input:
nums = [1,2]
›
Output:
1.5
💡 Note:
Only one pair possible: (1,2) → average = (1+2)/2 = 1.5. This is the minimum and only average.
Constraints
- 2 ≤ nums.length ≤ 50
- nums.length is even
- 1 ≤ nums[i] ≤ 50
- All elements are positive integers
Visualization
Tap to expand
Understanding the Visualization
1
Line Up Players
Sort all players by skill level from weakest to strongest
2
Pair Extremes
Always pair the weakest remaining player with the strongest remaining player
3
Calculate Team Strength
The team strength is the average of both players' skills
4
Track Minimum
Keep track of the weakest team strength across all pairings
Key Takeaway
🎯 Key Insight: Sort the array once to avoid repeatedly searching for minimum and maximum elements, reducing time complexity from O(n²) to O(n log n)
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code