Minimum Average of Smallest and Largest Elements
EASYDescription
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.
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 <= 50nis even.1 <= nums[i] <= 50
Approaches
Checkout 3 different approaches to solve Minimum Average of Smallest and Largest Elements. Click on different approaches to view the approach and algorithm in detail.
Brute-Force Simulation
This approach directly simulates the process described in the problem. We use a data structure that allows for dynamic resizing and element removal, such as an ArrayList. In each step, we iterate through the current list to find the minimum and maximum elements, calculate their average, and then remove them. We repeat this process until the list is empty, keeping track of the minimum average found.
Algorithm
- Convert the input array
numsinto aList<Integer>to facilitate easy element removal. - Initialize a variable
minAveragetoDouble.MAX_VALUE. - Loop
n / 2times, wherenis the initial size ofnums. - In each iteration:
- Find the minimum (
minElement) and maximum (maxElement) values in the current list. This can be done by iterating through the list or usingCollections.min()andCollections.max(). - Calculate the average:
currentAverage = (minElement + maxElement) / 2.0. - Update
minAverage = Math.min(minAverage, currentAverage). - Remove the
minElementandmaxElementfrom the list. Note that removing by value requires careful handling, especially if there are duplicates. It's safer to remove by index or uselist.remove(Integer.valueOf(value)).
- Find the minimum (
- After the loop completes, return
minAverage.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Solution {
public double minimumAverage(int[] nums) {
List<Integer> list = new ArrayList<>();
for (int num : nums) {
list.add(num);
}
double minAverage = Double.MAX_VALUE;
int n = nums.length;
for (int i = 0; i < n / 2; i++) {
int minElement = Collections.min(list);
int maxElement = Collections.max(list);
double currentAverage = (minElement + maxElement) / 2.0;
minAverage = Math.min(minAverage, currentAverage);
// Remove one occurrence of minElement and maxElement
list.remove(Integer.valueOf(minElement));
list.remove(Integer.valueOf(maxElement));
}
return minAverage;
}
}
Complexity Analysis
Pros and Cons
- Simple to understand and implement as it directly follows the problem statement.
- Inefficient due to repeated searches for min/max and costly removal operations.
Code Solutions
Checking out 3 solutions in different languages for Minimum Average of Smallest and Largest Elements. Click on different languages to view the code.
class Solution {
public
double minimumAverage(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
int ans = 1 << 30;
for (int i = 0; i < n / 2; ++i) {
ans = Math.min(ans, nums[i] + nums[n - i - 1]);
}
return ans / 2.0;
}
}
Video Solution
Watch the video walkthrough for Minimum Average of Smallest and Largest Elements
Similar Questions
5 related questions you might find useful
Algorithms:
Patterns:
Data Structures:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.