You are given an array of unique integers salary
where salary[i]
is the salary of the ith
employee.
Return the average salary of employees excluding the minimum and maximum salary. Answers within 10-5
of the actual answer will be accepted.
Example 1:
Input: salary = [4000,3000,1000,2000] Output: 2500.00000 Explanation: Minimum salary and maximum salary are 1000 and 4000 respectively. Average salary excluding minimum and maximum salary is (2000+3000) / 2 = 2500
Example 2:
Input: salary = [1000,2000,3000] Output: 2000.00000 Explanation: Minimum salary and maximum salary are 1000 and 3000 respectively. Average salary excluding minimum and maximum salary is (2000) / 1 = 2000
Constraints:
3 <= salary.length <= 100
1000 <= salary[i] <= 106
salary
are unique.This problem asks to calculate the average salary of employees, excluding the highest and lowest salaries. The most efficient approach involves a single pass through the array.
Algorithm:
Initialization: Initialize variables to track the minimum salary (min_salary
), maximum salary (max_salary
), and the sum of all salaries (total_salary
). Set min_salary
to a large value (e.g., infinity) and max_salary
to a small value (e.g., negative infinity) to ensure the first element correctly updates them.
Iteration: Iterate through the salary
array. For each salary:
min_salary
to the minimum of min_salary
and the current salary.max_salary
to the maximum of max_salary
and the current salary.total_salary
.Calculation: After iterating through the entire array:
min_salary
and max_salary
from total_salary
to exclude them.(length of salary array - 2)
to get the average salary (excluding the minimum and maximum).Time Complexity: O(n), where n is the length of the salary
array. We iterate through the array once.
Space Complexity: O(1). We use a constant number of extra variables regardless of the input size.
Code Implementation (Python):
class Solution:
def average(self, salary: List[int]) -> float:
min_salary = float('inf')
max_salary = float('-inf')
total_salary = 0
for salary_value in salary:
min_salary = min(min_salary, salary_value)
max_salary = max(max_salary, salary_value)
total_salary += salary_value
return (total_salary - min_salary - max_salary) / (len(salary) - 2)
Code Implementation (Java):
class Solution {
public double average(int[] salary) {
int minSalary = Integer.MAX_VALUE;
int maxSalary = Integer.MIN_VALUE;
int totalSalary = 0;
for (int sal : salary) {
minSalary = Math.min(minSalary, sal);
maxSalary = Math.max(maxSalary, sal);
totalSalary += sal;
}
return (double)(totalSalary - minSalary - maxSalary) / (salary.length - 2);
}
}
Code Implementation (C++):
class Solution {
public:
double average(vector<int>& salary) {
int minSalary = INT_MAX;
int maxSalary = INT_MIN;
int totalSalary = 0;
for (int sal : salary) {
minSalary = min(minSalary, sal);
maxSalary = max(maxSalary, sal);
totalSalary += sal;
}
return (double)(totalSalary - minSalary - maxSalary) / (salary.size() - 2);
}
};
The other language implementations (Go, TypeScript, Rust, PHP, C) follow a very similar structure, adapting the syntax specific to each language, but maintaining the same core logic and time/space complexity. The essential difference is how they handle integer vs. floating point math and the initial values for min/max.