Problem Description
Explanation
To solve this problem, we first need to sort the array. Then we calculate the number of elements to remove from the start and end of the array (5% of the total elements in each case). After removing these elements, we calculate the mean of the remaining elements.
Solutions
class Solution {
public double trimMean(int[] arr) {
Arrays.sort(arr);
int n = arr.length;
int removeCount = n / 20;
double sum = 0;
for (int i = removeCount; i < n - removeCount; i++) {
sum += arr[i];
}
return sum / (n - 2 * removeCount);
}
}
Loading editor...