Minimum Average of Smallest and Largest Elements - Problem
You have an array of floating point numbers averages which is initially empty. You are given an array nums of n integers where n is even.
You repeat the following procedure n / 2 times:
- Remove the smallest element,
minElement, and the largest elementmaxElement, fromnums. - Add
(minElement + maxElement) / 2toaverages.
Return the minimum element in averages.
Input & Output
Example 1 — 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)→8.0, (3,13)→8.0, (4,8)→6.0, (4,7)→5.5. Minimum average is 5.5.
Example 2 — Even Distribution
$
Input:
nums = [1,9,8,2,3,7]
›
Output:
5.0
💡 Note:
After sorting: [1,2,3,7,8,9]. Pairs: (1,9)→5.0, (2,8)→5.0, (3,7)→5.0. All averages are 5.0, so minimum is 5.0.
Example 3 — Minimum Size
$
Input:
nums = [1,2]
›
Output:
1.5
💡 Note:
Only one pair possible: (1,2) with average (1+2)/2 = 1.5.
Constraints
- 2 ≤ nums.length ≤ 105
- nums.length is even
- 1 ≤ nums[i] ≤ 105
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Given array of even length integers
2
Pair Extremes
Repeatedly pair smallest with largest
3
Find Minimum
Return minimum average computed
Key Takeaway
🎯 Key Insight: Sorting allows us to efficiently pair minimum and maximum elements using two pointers, avoiding repeated searches.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code