Skip to content

Commit dc67989

Browse files
authored
Merge pull request #1910 from Saipradyumnagoud/main
Added 3194-minimum-average-of-smallest-and-largest-elements
2 parents e99ef83 + 968499c commit dc67989

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
---
2+
id: minimum-average-of-smallest-and-largest-elements
3+
title: Minimum Average of Smallest and Largest Elements
4+
sidebar_label: Minimum Average of Smallest and Largest Elements
5+
tags:
6+
- Array
7+
- Sorting
8+
- Two Pointers
9+
- Java
10+
- Python
11+
description: "This document provides solutions for the Minimum Average of Smallest and Largest Elements problem."
12+
---
13+
14+
## Problem Statement
15+
16+
Given an integer array `nums`, find the minimum possible average of any sublist of `nums` after sorting the array in non-decreasing order.
17+
18+
**Example:**
19+
20+
Input: `nums = [3, 1, 2, 4, 3]`
21+
Output: `3.0`
22+
23+
Explanation:
24+
Sort the array to get `[1, 2, 3, 3, 4]`.
25+
Sublists with minimum average:
26+
27+
```
28+
- [1, 2, 3] -> Average = (1 + 2 + 3) / 3 = 2.0
29+
- [2, 3, 3] -> Average = (2 + 3 + 3) / 3 = 2.67
30+
- [3, 3, 4] -> Average = (3 + 3 + 4) / 3 = 3.33
31+
```
32+
33+
The minimum average is `2.0`.
34+
35+
**Constraints:**
36+
37+
- `1 <= nums.length <= 100`
38+
- `-10^5 <= nums[i] <= 10^5`
39+
40+
## Solutions
41+
42+
### Intuition
43+
44+
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.
45+
46+
### Approach
47+
48+
1. **Sort the Array:**
49+
- Sort the given array `nums` in non-decreasing order.
50+
51+
2. **Two Pointers Technique:**
52+
- Use two pointers `left` starting at the beginning (`0`) and `right` at the end (`nums.length - 1`) of the sorted array.
53+
- Calculate the average of `nums[left]` and `nums[right]`.
54+
- Update the minimum average if the current average is smaller.
55+
- Move `left` pointer to the right and `right` pointer to the left to consider the next pair of smallest and largest elements.
56+
57+
3. **Compute Minimum Average:**
58+
- Iterate until `left` is less than `right`.
59+
- The minimum average found during iterations is the desired result.
60+
61+
### Java
62+
63+
```java
64+
import java.util.Arrays;
65+
66+
class Solution {
67+
public double minimumAverage(int[] nums) {
68+
Arrays.sort(nums);
69+
int left = 0, right = nums.length - 1;
70+
double minAvg = Double.MAX_VALUE;
71+
72+
while (left < right) {
73+
double avg = (double) (nums[left] + nums[right]) / 2;
74+
minAvg = Math.min(minAvg, avg);
75+
left++;
76+
right--;
77+
}
78+
79+
return minAvg;
80+
}
81+
}
82+
```
83+
84+
### Python
85+
86+
```Python
87+
class Solution:
88+
def minimumAverage(self, nums: List[int]) -> float:
89+
nums.sort()
90+
left, right = 0, len(nums) - 1
91+
min_avg = float('inf')
92+
93+
while left < right:
94+
avg = (nums[left] + nums[right]) / 2.0
95+
min_avg = min(min_avg, avg)
96+
left += 1
97+
right -= 1
98+
99+
return min_avg
100+
```
101+
102+
## Conclusion
103+
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.

0 commit comments

Comments
 (0)