LeetCode Problem Workspace
Mean of Array After Removing Some Elements
Calculate the trimmed mean by removing the lowest and highest 5% of elements in an array using sorting for accuracy and speed.
2
Topics
6
Code langs
3
Related
Practice Focus
Easy · Array plus Sorting
Answer-first summary
Calculate the trimmed mean by removing the lowest and highest 5% of elements in an array using sorting for accuracy and speed.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Array plus Sorting
Start by sorting the array to identify and remove the smallest 5% and largest 5% of elements. After trimming, sum the remaining numbers and divide by their count to compute the mean. This method ensures precision and avoids errors caused by including extreme outliers in the calculation.
Problem Statement
Given an integer array arr of length divisible by 20, return the mean of the remaining integers after removing the smallest 5% and largest 5% of elements. Answers within 10^-5 of the actual mean will be accepted.
For example, if arr = [1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3], removing the minimum and maximum elements leaves all values equal to 2, so the mean is 2.00000. The array may contain zeros and large values up to 105.
Examples
Example 1
Input: arr = [1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3]
Output: 2.00000
After erasing the minimum and the maximum values of this array, all elements are equal to 2, so the mean is 2.
Example 2
Input: arr = [6,2,7,5,1,2,0,3,10,2,5,0,5,5,0,8,7,6,8,0]
Output: 4.00000
Example details omitted.
Example 3
Input: arr = [6,0,7,0,7,5,7,8,3,4,0,7,8,1,6,8,1,1,2,4,8,1,9,5,4,3,8,5,10,8,6,6,1,0,6,10,8,2,3,4]
Output: 4.77778
Example details omitted.
Constraints
- 20 <= arr.length <= 1000
- arr.length is a multiple of 20.
- 0 <= arr[i] <= 105
Solution Approach
Sort the Array
Sorting arr in ascending order allows direct removal of the smallest 5% and largest 5% elements. This step ensures that extreme values do not distort the mean calculation.
Trim 5% from Both Ends
Compute 5% of arr.length as the count to remove from each end. Slice the sorted array to discard these extremes and isolate the central values that contribute to the final mean.
Compute the Mean
Sum the remaining array elements and divide by the new length. Ensure floating-point division is used to avoid truncation errors and return a result with at least five decimal precision.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Sorting dominates the time complexity, giving O(n log n), while trimming and summing the array is O(n). Space complexity is O(1) if done in-place or O(n) if creating a new array slice.
What Interviewers Usually Probe
- Emphasizes array sorting and numerical precision.
- May check understanding of percentage-based element removal.
- Looks for efficient handling of small arrays with high-value extremes.
Common Pitfalls or Variants
Common pitfalls
- Forgetting to use floating-point division when calculating the mean.
- Incorrectly rounding 5% to a non-integer number of elements.
- Including trimmed extreme elements in the mean calculation by mistake.
Follow-up variants
- Remove a custom percentage of elements from both ends instead of 5%.
- Compute the median after trimming extremes rather than the mean.
- Apply the same trimming approach to multidimensional arrays per row.
FAQ
What is the best way to compute the mean after removing extremes?
Sort the array, remove the smallest and largest 5% of elements, then sum the rest and divide by the count using floating-point arithmetic.
How does the pattern 'Array plus Sorting' apply here?
Sorting is essential to quickly locate and remove extreme elements, which is the central step of the 'Array plus Sorting' pattern.
What precision is required for the answer?
Answers within 10^-5 of the true mean are considered correct, so maintain at least five decimal places.
Can I solve this without sorting?
While theoretically possible using selection algorithms, sorting simplifies finding the top and bottom 5% efficiently and is preferred for this problem.
Does this method work for arrays with zeros and duplicates?
Yes, sorting correctly identifies extremes even if multiple elements have the same value, ensuring the trimmed mean is accurate.
Solution
Solution 1
#### Python3
class Solution:
def trimMean(self, arr: List[int]) -> float:
n = len(arr)
start, end = int(n * 0.05), int(n * 0.95)
arr.sort()
t = arr[start:end]
return round(sum(t) / len(t), 5)Continue Topic
array
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Array plus Sorting
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward