LeetCode 1619: Mean of Array After Removing Some Elements Solution

Master LeetCode problem 1619 (Mean of Array After Removing Some Elements), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1619. Mean of Array After Removing Some Elements

Problem Explanation

Explanation

To solve this problem, we first need to sort the array. Then we calculate the number of elements to remove from the start and end of the array (5% of the total elements in each case). After removing these elements, we calculate the mean of the remaining elements.

Solution Code

class Solution {
    public double trimMean(int[] arr) {
        Arrays.sort(arr);
        int n = arr.length;
        int removeCount = n / 20;
        double sum = 0;
        
        for (int i = removeCount; i < n - removeCount; i++) {
            sum += arr[i];
        }
        
        return sum / (n - 2 * removeCount);
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1619 (Mean of Array After Removing Some Elements)?

This page provides optimized solutions for LeetCode problem 1619 (Mean of Array After Removing Some Elements) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1619 (Mean of Array After Removing Some Elements)?

The time complexity for LeetCode 1619 (Mean of Array After Removing Some Elements) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1619 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1619 in Java, C++, or Python.

Back to LeetCode Solutions