Skip to main content

Minimum Average of Smallest and Largest Elements - Solution & Explanation

EasyArrayTwo PointersSorting12 min readAsked at: Amazon, Google
Practice this problem

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

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.

Approach 1: Sorting Based Approach

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n log n) due to sorting.
Space Complexity: O(1) since we sort in place.

Try this approach in the editor →

Approach 2: Using Min and Max Heaps

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.

Code

Python

C++

Complexity

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.

Try this approach in the editor →

Approach 3: Sorting

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.

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Sorting Based Approach

Time Complexity: O(n log n) due to sorting.
Space Complexity: O(1) since we sort in place.

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.
Space Complexity: O(n) for the heaps.

Sorting—

Detailed 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

Video Solution

3194 Minimum Average of Smallest and Largest Elements || How to 🤔 in Interview || Sort || 2 Pointer • Ayush Rao • 1,012 views views

Watch 5 more video solutions →

Frequently Asked Questions

Is Minimum Average of Smallest and Largest Elements easy or hard?
Minimum Average of Smallest and Largest Elements is classified as an Easy problem. The logic mainly tests understanding of sorting and the two-pointer technique rather than complex data structures or advanced algorithms.
Minimum Average of Smallest and Largest Elements Python/Java solution
In Python or Java, sort the array first, then maintain two indices at the start and end. Compute (nums[left] + nums[right]) / 2 at each step, update the minimum average, and move the pointers inward until they cross. This implementation runs in O(n log n) time.
How to solve Minimum Average of Smallest and Largest Elements in O(n)?
An O(n) solution is generally not possible for arbitrary input because identifying smallest and largest pairs requires ordering the elements. Sorting introduces an O(n log n) lower bound for comparison-based algorithms. After sorting, the pairing itself runs in linear time.
What is the best approach for Minimum Average of Smallest and Largest Elements?
The most efficient and commonly expected solution sorts the array and then uses two pointers from both ends. After sorting, pair nums[left] and nums[right], compute their average, and move the pointers inward. This approach runs in O(n log n) time due to sorting and uses O(1) extra space.
Is Minimum Average of Smallest and Largest Elements asked at Google/Amazon/Meta?
Problems combining sorting with two-pointer pairing frequently appear in interviews at companies like Amazon, Google, and Meta. While this exact problem may vary, the pattern of sorting and scanning from both ends is a common interview technique.
What data structure is used in Minimum Average of Smallest and Largest Elements?
The typical solution uses arrays with a sorting algorithm and a two-pointer technique. An alternative implementation uses a min heap and a max heap to repeatedly extract the smallest and largest elements.
What is the time complexity of Minimum Average of Smallest and Largest Elements?
The standard solution using sorting and two pointers has O(n log n) time complexity because the array must be sorted first. The pairing step afterward is a single linear pass O(n). Space complexity is O(1) if the sort is performed in place.

Ready to solve this problem?

Practice Minimum Average of Smallest and Largest Elements with our built-in code editor and test cases.

Practice on FleetCode