Average Salary Excluding the Minimum and Maximum Salary

Here’s a simple step-by-step guide to solving this problem:

  1. First, find the maximum and minimum values in the salary array.
  2. Next, sum up all the values in the salary array and subtract the maximum and minimum values from this sum.
  3. Finally, divide this sum by the number of employees minus 2 (to account for the removed max and min salaries).

This will give the average salary of the employees excluding the maximum and minimum salary.

Python solution:

1
2
3
4
5
6
class Solution:
    def average(self, salary):
        min_salary = min(salary)
        max_salary = max(salary)
        total = sum(salary) - min_salary - max_salary
        return total / (len(salary) - 2)

This solution works in linear time complexity, i.e., O(n), where n is the number of employees. It meets the constraints 3 <= salary.length <= 100, 1000 <= salary[i] <= 106, and all salary values are unique.