Mean of Array After Removing Some Elements Leetcode Solution


Difficulty Level Easy
Frequently asked in Google
Array

Problem statement

In the problem ” Mean of Array After Removing Some Elements” we are given an array. All the elements of the array are positive integers. The size of the array is multiple of 20.

Our task is to find the mean of the array excluding the smallest 5% elements and the highest 5% elements.

Example

arr = [6,2,7,5,1,2,0,3,10,2,5,0,5,5,0,8,7,6,8,0]
4.000000

Explanation:

Mean of Array After Removing Some Elements Leetcode Solution

The image shows the given array after sorting. After removing the highest and lowest 5% elements, the sum of the remaining element is 172, and the total number of remaining elements are 36. So the mean becomes 4.77778.

Approach for Mean of Array After Removing Some Elements Leetcode Solution

This is an easy implementation problem. Below are the steps to solve this problem:

  1. Sort the given array as we need to trim the highest and lowest 5% elements from the array.
  2. Store the length of the array in a variable.
  3. Declare two variables:
    1. To store the sum of elements.
    2. To store the number of elements in-between.
  4. Now run a loop and find the sum of the trimmed elements.
    1. For this, we will initialize the loop from n*0.05 where n is the length of the given array and the loop will run till n*0.95.
  5. Calculate the average.
    1.  An important point to note here is that it should return a double value, not an integer value.

Implementation

C++ code for Mean of Array After Removing Some Elements

#include <bits/stdc++.h> 
using namespace std; 
    double trimMean(vector<int>& arr) {
        sort(arr.begin(),arr.end());
        int n=arr.size();
        double sum=0,cnt=0;
        for(int i=(n*.05);i<n*.95;i++)
        { 
            sum+=arr[i];
            cnt++;
        }
        return sum/cnt;      
    }
int main() 
{ 
 vector<int> 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}; 
 double ans=trimMean(arr);
 cout<<ans<<endl;
 return 0;
}
4.77778

Java code for Mean of Array After Removing Some Elements

import java.util.Arrays; 
public class Tutorialcup {
    public static double trimMean(int[] arr) {
     Arrays.sort(arr);
     int n = arr.length;   
     double sum = 0d,cnt=0; 
     for(int i=n / 20;i<n - n / 20;i++)
        { 
            sum+=arr[i];
            cnt++;
        }
        return sum/cnt; 
    }
  public static void main(String[] args) {
        int [] 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}; 
        double ans=trimMean(arr); 
        System.out.println(ans);
  }
}
4.77778

Complexity Analysis of Mean of Array After Removing Some Elements Leetcode Solution

Time complexity

The time complexity of the above code is O(nlogn) because we are sorting the given array. Here n is the length of the given array.

Space complexity

The space complexity of the above code is O(1) because we are using only a variable to store answer.

References

Array Interview Questions
Graph Interview Questions
LinkedList Interview Questions
String Interview Questions
Tree Interview Questions
Core Java Interview Questions