3194. Minimum Average of Smallest and Largest Elements LeetCode Solution
In this guide, you will get 3194. Minimum Average of Smallest and Largest Elements LeetCode Solution with the best time and space complexity. The solution to Minimum Average of Smallest and Largest Elements problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Average of Smallest and Largest Elements solution in C++
- Minimum Average of Smallest and Largest Elements solution in Java
- Minimum Average of Smallest and Largest Elements solution in Python
- Additional Resources
Problem Statement of Minimum Average of Smallest and Largest Elements
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
Complexity Analysis
- Time Complexity: O(\texttt{sort})
- Space Complexity: O(\texttt{sort})
3194. Minimum Average of Smallest and Largest Elements LeetCode Solution in C++
class Solution {
public:
double minimumAverage(vector<int>& nums) {
constexpr int kMax = 50;
double ans = kMax;
int i = 0;
int j = nums.size() - 1;
ranges::sort(nums);
while (i < j)
ans = min(ans, (nums[i++] + nums[j--]) / 2.0);
return ans;
}
};
/* code provided by PROGIEZ */
3194. Minimum Average of Smallest and Largest Elements LeetCode Solution in Java
class Solution {
public double minimumAverage(int[] nums) {
final int kMax = 50;
double ans = kMax;
int i = 0;
int j = nums.length - 1;
Arrays.sort(nums);
while (i < j)
ans = Math.min(ans, (nums[i++] + nums[j--]) / 2.0);
return ans;
}
}
// code provided by PROGIEZ
3194. Minimum Average of Smallest and Largest Elements LeetCode Solution in Python
class Solution:
def minimumAverage(self, nums: list[int]) -> float:
nums.sort()
return min((nums[i] + nums[~i]) / 2
for i in range(len(nums) // 2 + 1))
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.