Skip to main content

Minimum Average of Smallest and Largest Elements - Video Solutions

EasyArrayTwo PointersSorting

3194 Minimum Average of Smallest and Largest Elements || How to 🤔 in Interview || Sort || 2 Pointer

Ayush Rao
4:571,012 views
6 video solutions available

Minimum Average of Smallest and Largest Elements - Video Solution

Watch 6 video solutions for Minimum Average of Smallest and Largest Elements, a easy level problem involving Array, Two Pointers, Sorting. This walkthrough by Ayush Rao has 1,012 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

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 element maxElement, from nums.
  • Add (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]
The smallest element of averages, 5.5, is returned.

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 <= 50
  • n is even.
  • 1 <= nums[i] <= 50
Read full problem with examples

Approach Overview

Problem 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.

Complexity Analysis

ApproachTimeSpaceWhen to Use
Sorting + Two PointersO(n log n)O(1)Best general solution; simple implementation and minimal memory
Min Heap + Max HeapO(n log n)O(n)Useful when repeatedly accessing smallest and largest values dynamically