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.
Example:
salary = [4000, 3000, 1000, 2000]
2500.00000
To solve this problem, we can follow these steps:
Here is the algorithm:
The sorting step will dominate the time complexity, which will be O(n log n), where n
is the number of elements in the array. Other operations (finding the sum and average) are linear, i.e., O(n).
Here is the implementation of the strategy:
def average(salary):
# Sort the salary list to find min and max easily
salary.sort()
# Excluding the first and last element (which are min and max)
sum_of_salaries = sum(salary[1:-1])
count_of_salaries = len(salary) - 2
# Calculating average
return sum_of_salaries / count_of_salaries
# Example test case
salary = [4000, 3000, 1000, 2000]
print(average(salary)) # Output: 2500.0
By following this approach, the code efficiently excludes the minimum and maximum salaries and computes the average of the rest.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?