LeetCode 1491: Average Salary Excluding the Minimum and Maximum Salary Solution

Master LeetCode problem 1491 (Average Salary Excluding the Minimum and Maximum Salary), 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.

1491. Average Salary Excluding the Minimum and Maximum Salary

Problem Explanation

Explanation:

To find the average salary excluding the minimum and maximum salary, we can first find the minimum and maximum salaries in the array. Then, we iterate through the array to calculate the sum of all salaries excluding the minimum and maximum salaries. Finally, we divide the sum by the number of employees minus 2 to get the average salary.

  1. Find the minimum and maximum salaries in the array.
  2. Iterate through the array to calculate the sum of all salaries excluding the minimum and maximum.
  3. Calculate the average salary by dividing the sum by the number of employees minus 2.

Time Complexity: O(n) where n is the number of elements in the salary array.

Space Complexity: O(1)

:

Solution Code

class Solution {
    public double average(int[] salary) {
        int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
        double sum = 0;
        
        for (int s : salary) {
            sum += s;
            min = Math.min(min, s);
            max = Math.max(max, s);
        }
        
        return (sum - min - max) / (salary.length - 2);
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1491 (Average Salary Excluding the Minimum and Maximum Salary)?

This page provides optimized solutions for LeetCode problem 1491 (Average Salary Excluding the Minimum and Maximum Salary) 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 1491 (Average Salary Excluding the Minimum and Maximum Salary)?

The time complexity for LeetCode 1491 (Average Salary Excluding the Minimum and Maximum Salary) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1491 on DevExCode?

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

Back to LeetCode Solutions