Leetcode – Mean of Array After Removing Some Elements Solution

Spread the love

In the realms of data science, data preprocessing is a crucial step before performing any advanced analytics or computations. One such preprocessing step is the removal of outliers, which is precisely what the “Mean of Array After Removing Some Elements” problem from Leetcode mimics. This problem provides an opportunity to exercise array manipulation techniques and also opens doors to deeper statistical concepts like mean and percentile.

In this extensive article, we’ll dissect the problem description, explore multiple approaches to solve it, analyze their time and space complexity, and implement the solutions in Python. Along the way, we will also dig into the statistical concepts used, as well as any related problems.

Problem Description

Here’s the problem statement as given on Leetcode:

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.


trimMean([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) # Output: 5.5

Approach 1: Sorting and Slicing


  1. Sort the given array arr.
  2. Calculate the number of elements to be removed from both ends, which is 5% of the array length.
  3. Slice the sorted array to remove these elements.
  4. Calculate the mean of the remaining array.

Python Code

from typing import List

def trimMean(arr: List[int]) -> float:
    n = len(arr)
    k = int(n * 0.05)
    trimmed_arr = arr[k:n-k]
    mean_value = sum(trimmed_arr) / len(trimmed_arr)
    return mean_value

Time Complexity

Sorting the array takes O(n log⁡ n) time, where n is the length of the array. Slicing and summing take O(n). So the overall time complexity is O(n log⁡ n).

Space Complexity

The space complexity is O(n) for storing the sorted and trimmed array.

Approach 2: Partial Sorting


  1. Instead of sorting the entire array, you can partially sort it to get the smallest 5% and largest 5% of elements.
  2. Remove these elements and calculate the mean of the remaining elements.

Python Code

import heapq

def trimMean(arr: List[int]) -> float:
    n = len(arr)
    k = int(n * 0.05)
    smallest_elements = heapq.nsmallest(k, arr)
    largest_elements = heapq.nlargest(k, arr)
    remaining_elements = sum(arr) - sum(smallest_elements) - sum(largest_elements)
    mean_value = remaining_elements / (n - 2 * k)
    return mean_value

Time Complexity

The time complexity for finding the smallest and largest k elements using heaps is O(n log⁡ k). Therefore, the overall time complexity becomes O(n log⁡ k).

Space Complexity

The space complexity is O(k) for storing the smallest and largest elements.

Statistical Insights

This problem implicitly introduces the concept of trimming outliers in a dataset before calculating its mean. By removing the smallest 5% and largest 5% of the data, you’re essentially eliminating elements that could potentially skew the average, providing a more “central” value. This technique is common in robust statistics.

Related Problems

  1. Find Median from Data Stream: This problem extends the concept of finding the “middle” value in a stream of integers.
  2. Moving Average from Data Stream: This problem involves calculating the moving average of a stream of integers.


Both the sorting and partial sorting approaches provide a good balance of efficiency and readability. The choice between them will depend on the specific requirements of the application. This problem also serves as a good introduction to some statistical preprocessing steps that are often required in real-world data science projects. Overall, it’s a well-rounded problem that challenges both your algorithmic skills and your statistical thinking.

Leave a Reply