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:

  1. Start with an empty averages array
  2. Repeat n/2 times:
    • Find and remove the smallest element (minElement)
    • Find and remove the largest element (maxElement)
    • Calculate (minElement + maxElement) / 2.0 and add to averages
  3. 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
Tournament Pairing StrategyPlayers sorted by skill level:1344781315WeakestStrongestPairing Process:115Team 1Avg: 8.0313Team 2Avg: 8.048Team 3Avg: 6.047Team 4Avg: 5.5 ⭐Result: Minimum team strength = 5.5Algorithm: Sort once O(n log n), then pair extremes O(n)Two pointers move from both ends toward centerMuch faster than finding min/max repeatedly O(n²)🎯 Key Insight: Sorting eliminates repeated searches!
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)
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
28.2K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen