Minimum Average of Smallest and Largest Elements
Problem Statementβ
Given an integer array nums
, find the minimum possible average of any sublist of nums
after sorting the array in non-decreasing order.
Example:
Input: nums = [3, 1, 2, 4, 3]
Output: 3.0
Explanation:
Sort the array to get [1, 2, 3, 3, 4]
.
Sublists with minimum average:
- [1, 2, 3] -> Average = (1 + 2 + 3) / 3 = 2.0
- [2, 3, 3] -> Average = (2 + 3 + 3) / 3 = 2.67
- [3, 3, 4] -> Average = (3 + 3 + 4) / 3 = 3.33
The minimum average is 2.0
.
Constraints:
1 <= nums.length <= 100
-10^5 <= nums[i] <= 10^5
Solutionsβ
Intuitionβ
To find the minimum average of any sublist after sorting, we can leverage the property that the sublist with the minimum average will consist of the smallest and largest elements from the sorted array.
Approachβ
-
Sort the Array:
- Sort the given array
nums
in non-decreasing order.
- Sort the given array
-
Two Pointers Technique:
- Use two pointers
left
starting at the beginning (0
) andright
at the end (nums.length - 1
) of the sorted array. - Calculate the average of
nums[left]
andnums[right]
. - Update the minimum average if the current average is smaller.
- Move
left
pointer to the right andright
pointer to the left to consider the next pair of smallest and largest elements.
- Use two pointers
-
Compute Minimum Average:
- Iterate until
left
is less thanright
. - The minimum average found during iterations is the desired result.
- Iterate until
Javaβ
import java.util.Arrays;
class Solution {
public double minimumAverage(int[] nums) {
Arrays.sort(nums);
int left = 0, right = nums.length - 1;
double minAvg = Double.MAX_VALUE;
while (left < right) {
double avg = (double) (nums[left] + nums[right]) / 2;
minAvg = Math.min(minAvg, avg);
left++;
right--;
}
return minAvg;
}
}
Pythonβ
class Solution:
def minimumAverage(self, nums: List[int]) -> float:
nums.sort()
left, right = 0, len(nums) - 1
min_avg = float('inf')
while left < right:
avg = (nums[left] + nums[right]) / 2.0
min_avg = min(min_avg, avg)
left += 1
right -= 1
return min_avg
Conclusionβ
The above implementations in Java and Python effectively find the minimum possible average of any sublist of nums after sorting using the two pointers technique, ensuring efficient computation.