Skip to content

Commit 2fe97f4

Browse files
solves #3194: Minimum Average of Smallest and Largest Elements in java
1 parent d658404 commit 2fe97f4

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,6 @@
920920
| 3178 | [Find the Child Who Has the Ball After K Seconds](https://leetcode.com/problems/find-the-child-who-has-the-ball-after-k-seconds) | [![Java](assets/java.png)](src/FindTheChildWhoHasTheBallAfterKSeconds.java) | |
921921
| 3184 | [Count Pairs That Form a Complete Day I](https://leetcode.com/problems/count-pairs-that-form-a-complete-day-i) | [![Java](assets/java.png)](src/CountPairsThatFormACompleteDayI.java) | |
922922
| 3190 | [Find Minimum Operations to Make All Elements Divisible by Three](https://leetcode.com/problems/find-minimum-operations-to-make-all-elements-divisible-by-three) | [![Java](assets/java.png)](src/FindMinimumOperationsToMakeAllElementsDivisibleByThree.java) | |
923-
| 3194 | [Minimum Average of Smallest and Largest Elements](https://leetcode.com/problems/minimum-average-of-smallest-and-largest-elements) | | |
923+
| 3194 | [Minimum Average of Smallest and Largest Elements](https://leetcode.com/problems/minimum-average-of-smallest-and-largest-elements) | [![Java](assets/java.png)](src/MinimumAverageOfSmallestAndLargestElements.java) | |
924924
| 3199 | [Count Triplets With Even XOR Set Bits I](https://leetcode.com/problems/count-triplets-with-even-xor-set-bits-i) | | |
925925
| 3200 | [Maximum Height of a Triangle](https://leetcode.com/problems/maximum-height-of-a-triangle) | | |

Diff for: src/MinimumAverageOfSmallestAndLargestElements.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// https://leetcode.com/problems/minimum-average-of-smallest-and-largest-elements
2+
// T: O(N log(N))
3+
// S: O(log(N))
4+
5+
import java.util.Arrays;
6+
7+
public class MinimumAverageOfSmallestAndLargestElements {
8+
public double minimumAverage(int[] array) {
9+
Arrays.sort(array);
10+
double minAverage = Double.MAX_VALUE;
11+
12+
for (int i = 0 ; i < array.length / 2 ; i++) {
13+
minAverage = Math.min(minAverage, ((double) array[i] + array[array.length - i - 1]) / 2);
14+
}
15+
16+
return minAverage;
17+
}
18+
}

0 commit comments

Comments
 (0)