#1619
Easy Algorithms Mean of array after removing some elements
Array Sorting
71.5% acceptance
Feb 25, 2026
527
134
Given an integer array arr, return the mean of the remaining integers after removing the smallest 5% and the largest 5% of the elements.
Answers within 10-5 of the actual answer will be considered accepted.
Solution
Rust
Time O(n log n)
Space O(1)
impl Solution {
pub fn trim_mean(mut arr: Vec<i32>) -> f64 {
arr.sort();
let n = arr.len();
let trim = n / 20; // 5%
let sum: i32 = arr[trim..n-trim].iter().sum();
sum as f64 / (n - 2 * trim) as f64
}
}