Problem Description
Given an array of unique integers representing each employee's salary, return the average salary computed from the employees excluding the minimum and maximum salaries.
Key Insights
- All salary values are unique, so there is exactly one minimum and one maximum.
- Identify and exclude the minimum and maximum salaries.
- Compute the sum of the remaining salaries and divide it by their count.
Space and Time Complexity
Time Complexity: O(n), where n is the number of employees (single or double pass over the list). Space Complexity: O(1), as only a few extra variables are used.
Solution
The approach involves first determining the minimum and maximum salaries in the array using a single pass (or built-in methods). Then, by iterating through the array, we sum the values that are not equal to either the minimum or maximum. Finally, the average is computed by dividing the accumulated sum by the count of the salaries taken into account. This method uses constant extra space and efficiently computes the result in linear time.