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 <= 1001000 <= salary[i] <= 106salary are unique.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find the average salary excluding the highest and lowest, a brute force approach means we directly simulate the problem's requirements. We will find the maximum and minimum salaries, and then calculate the average of the remaining salaries. This is done by looking at each salary one at a time.
Here's how the algorithm would work step-by-step:
def average_salary_excluding_the_minimum_and_maximum_salary(salary):
maximum_salary = salary[0]
minimum_salary = salary[0]
# Find the maximum salary.
for individual_salary in salary:
if individual_salary > maximum_salary:
maximum_salary = individual_salary
# Find the minimum salary.
for individual_salary in salary:
if individual_salary < minimum_salary:
minimum_salary = individual_salary
salaries_sum = 0
number_of_valid_salaries = 0
# Sum the salaries, excluding the min and max.
for individual_salary in salary:
if individual_salary != maximum_salary and individual_salary != minimum_salary:
salaries_sum += individual_salary
number_of_valid_salaries += 1
# Handle the edge case where the list is empty, or all elements are same
if number_of_valid_salaries == 0:
return 0
return salaries_sum / number_of_valid_salariesTo find the average salary excluding the highest and lowest, we need to first identify these extreme values. Once identified, we can efficiently compute the sum of the remaining salaries and then divide by the count of those salaries to get the average.
Here's how the algorithm would work step-by-step:
def average_salary_excluding_extremes(salary):
minimum_salary = min(salary)
maximum_salary = max(salary)
salaries_sum = sum(salary)
# Exclude min and max salaries
salaries_sum -= (minimum_salary + maximum_salary)
# Recalculate salary count
adjusted_salary_count = len(salary) - 2
# Prevents division by zero
if adjusted_salary_count == 0:
return 0
# Calculate the average salary
average_salary = salaries_sum / adjusted_salary_count
return average_salary| Case | How to Handle |
|---|---|
| Null or empty input array | Return 0, or throw an IllegalArgumentException, as the average is undefined for an empty set of salaries. |
| Array with exactly two elements | After excluding the min and max, no salaries remain, so return 0 as the average of nothing is zero. |
| Array with identical values | The min and max will be the same value, so after removing them, the average will be the value itself. |
| Array with very large positive or negative numbers (integer overflow) | Use long to accumulate the sum to prevent integer overflow, especially if salaries are integers. |
| Array contains zero | The algorithm should work correctly with zero as a valid salary, since it's just a number. |
| Large input array (performance) | Ensure the solution has a linear time complexity, such as iterating through the array once to find min/max and then calculating the average. |
| Floating-point precision errors when calculating the average | Use double to store the average to maintain precision, especially when dealing with large salary values. |
| Array containing only negative values | The algorithm should correctly identify the minimum and maximum (most negative) salary and exclude them. |