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:
minElement, and the largest element maxElement, from nums.(minElement + maxElement) / 2 to averages.Return the minimum element in averages.
Example 1:
Input: nums = [7,8,3,4,15,13,4,1]
Output: 5.5
Explanation:
| step | nums | averages |
|---|---|---|
| 0 | [7,8,3,4,15,13,4,1] | [] |
| 1 | [7,8,3,4,13,4] | [8] |
| 2 | [7,8,4,4] | [8,8] |
| 3 | [7,4] | [8,8,6] |
| 4 | [] | [8,8,6,5.5] |
Example 2:
Input: nums = [1,9,8,3,10,5]
Output: 5.5
Explanation:
| step | nums | averages |
|---|---|---|
| 0 | [1,9,8,3,10,5] | [] |
| 1 | [9,8,3,5] | [5.5] |
| 2 | [8,5] | [5.5,6] |
| 3 | [] | [5.5,6,6.5] |
Example 3:
Input: nums = [1,2,3,7,8,9]
Output: 5.0
Explanation:
| step | nums | averages |
|---|---|---|
| 0 | [1,2,3,7,8,9] | [] |
| 1 | [2,3,7,8] | [5] |
| 2 | [3,7] | [5,5] |
| 3 | [] | [5,5,5] |
Constraints:
2 <= n == nums.length <= 50n is even.1 <= nums[i] <= 50Problem Overview: You are given an integer array. Repeatedly take the smallest and largest remaining elements, compute their average (a + b) / 2, and track the minimum average among all such pairs. Each step removes both elements from consideration. The goal is to return the smallest average produced during this process.
The key observation: pairing the smallest and largest values naturally happens when the array is ordered. Once sorted, the smallest element sits at the left end and the largest at the right end, making the pairing step trivial.
Approach 1: Sorting + Two Pointers (O(n log n) time, O(1) extra space)
Sort the array first. After sorting, use two pointers: left starting at index 0 and right at the last index. At each step, compute the average of nums[left] and nums[right], update the running minimum, then move both pointers inward (left++, right--). Sorting guarantees that every iteration pairs the current smallest and largest remaining elements exactly as the problem requires. The dominant cost is sorting at O(n log n), while the two‑pointer scan runs in O(n). Extra space stays O(1) if sorting is done in place.
This approach relies on ideas from sorting and two pointers. Once the array is ordered, the algorithm becomes a simple linear sweep.
Approach 2: Min Heap + Max Heap (O(n log n) time, O(n) space)
Another way to simulate the process is by maintaining two heaps: a min heap to retrieve the smallest element and a max heap to retrieve the largest element. Insert all numbers into both structures. In each step, extract the minimum from the min heap and the maximum from the max heap, compute their average, and update the minimum result. This directly mirrors the "remove smallest and largest" rule.
Heap operations such as push and pop take O(log n) time, so building and processing the heaps leads to overall O(n log n) time complexity with O(n) extra space. This technique uses concepts from heap data structures and works well when you need dynamic access to extremes.
Recommended for interviews: The sorting + two pointers approach is what most interviewers expect. It directly models the pairing logic and keeps the implementation small and readable. Showing the heap approach demonstrates understanding of priority queues, but the sorted two‑pointer solution is cleaner and uses less memory.
The core idea across both methods is the same: always combine the smallest and largest remaining elements. Sorting simply provides the most efficient and straightforward way to enforce that pairing.
Sort the given array at the beginning. Use two pointers, one starting from the beginning of the array and the other from the end. In each step, calculate the average of elements pointed by these pointers, store it, and then move the pointers towards each other until they meet.
Start by sorting the array. Then, use two pointers to find min and max pairs and their averages. Track the minimum average found.
Time Complexity: O(n log n) due to sorting.
Space Complexity: O(1) since we sort in place.
Use a min heap to keep track of the smallest remaining element and a max heap to keep track of the largest. This efficient access to min/max allows us to compute averages efficiently without sorting.
Initialize min and max heaps. Pop the smallest and largest using heaps to find and calculate average efficiently. Track and return the minimum.
Time Complexity: O(n log n) since each insertion/removal from the heap is log n and this is done n times.
Space Complexity: O(n) for the heaps.
First, we sort the array nums. Then, we start taking elements from both ends of the array, calculating the sum of the two elements, and taking the minimum value. Finally, we return the minimum value divided by 2 as the answer.
The time complexity is O(n log n), and the space complexity is O(log n), where n is the length of the array nums.
| Approach | Complexity |
|---|---|
| Sorting Based Approach | Time Complexity: O(n log n) due to sorting. |
| Using Min and Max Heaps | Time Complexity: O(n log n) since each insertion/removal from the heap is log n and this is done n times. |
| Sorting | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sorting + Two Pointers | O(n log n) | O(1) | Best general solution; simple implementation and minimal memory |
| Min Heap + Max Heap | O(n log n) | O(n) | Useful when repeatedly accessing smallest and largest values dynamically |
LeetCode#3194 Minimum Average of Smallest and Largest Elements - Python • CodeJulian • 1,254 views views
Watch 5 more video solutions →Practice Minimum Average of Smallest and Largest Elements with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor