Mean of Array After Removing Some Elements - Problem

Given an integer array arr, you need to calculate the trimmed mean by removing outliers from both ends.

Specifically, you must:

  • Remove the smallest 5% of elements
  • Remove the largest 5% of elements
  • Calculate and return the mean (average) of the remaining elements

This technique is commonly used in statistics to reduce the impact of outliers on the final result, making it more representative of the central tendency.

Note: Answers within 10-5 of the actual answer will be considered correct.

Input & Output

example_1.py โ€” Basic Case
$ Input: arr = [1,2,3,4,5,6,7,8,9,10]
โ€บ Output: 5.5
๐Ÿ’ก Note: Array length is 10, so we remove 5% from each end (0.5 elements, rounded to 1). After sorting: [1,2,3,4,5,6,7,8,9,10]. Remove first element (1) and last element (10). Remaining elements: [2,3,4,5,6,7,8,9]. Sum = 44, Count = 8, Mean = 44/8 = 5.5
example_2.py โ€” Larger Array
$ Input: arr = [6,2,7,5,1,2,0,3,10,2,5,0,5,5,0,8,7,6,8,0]
โ€บ Output: 4.0
๐Ÿ’ก Note: Array length is 20, so we remove 5% from each end (1 element each). After sorting: [0,0,0,0,1,2,2,2,3,5,5,5,5,6,6,7,7,8,8,10]. Remove first element (0) and last element (10). Remaining 18 elements sum to 72. Mean = 72/18 = 4.0
example_3.py โ€” Edge Case
$ Input: arr = [9,7,8,7,7,8,4,4,6,8,8]
โ€บ Output: 7.18182
๐Ÿ’ก Note: Array length is 11, so we remove 5% from each end (0.55 elements, rounded to 0). No elements are removed. All elements are included in the mean calculation. Sum = 79, Count = 11, Mean = 79/11 โ‰ˆ 7.18182

Visualization

Tap to expand
๐ŸŠโ€โ™€๏ธ Olympic Diving: Trimmed Mean ScoringStep 1: Judge Scores (Unsorted)8.59.27.19.88.96.39.18.7Step 2: Sorted Scores (Low to High)6.37.18.58.78.99.19.29.8Step 3: Remove Extreme Scores (5% from each end)6.3Lowest7.18.58.78.99.19.29.8HighestStep 4: Final Score = (7.1+8.5+8.7+8.9+9.1+9.2) รท 6 = 8.58๐Ÿ† Official Score: 8.58 (Fair & Representative!)Trimmed Mean eliminates bias from extreme scores, just like Olympic judging!
Understanding the Visualization
1
Collect All Scores
Gather all the judge scores (array elements)
2
Arrange in Order
Sort scores from lowest to highest
3
Remove Extremes
Remove the lowest 5% and highest 5% of scores
4
Calculate Fair Average
Compute the mean of the remaining middle scores
Key Takeaway
๐ŸŽฏ Key Insight: Sorting allows us to easily identify and remove outliers, creating a more robust average that better represents the central tendency of the data.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

Finding min/max requires O(n) time, and we do this O(n) times in worst case

n
2n
โš  Quadratic Growth
Space Complexity
O(1)

Only using constant extra space for variables

n
2n
โœ“ Linear Space

Constraints

  • 20 โ‰ค arr.length โ‰ค 1000
  • arr.length is a multiple of 20
  • 0 โ‰ค arr[i] โ‰ค 105
  • At most 1% of test cases will have extreme values
Asked in
Amazon 35 Google 28 Microsoft 22 Apple 18
24.7K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen