## Similar Problems

Similar Problems not available

# Average Salary Excluding The Minimum And Maximum Salary - Leetcode Solution

## Companies:

LeetCode: Average Salary Excluding The Minimum And Maximum Salary Leetcode Solution

Difficulty: Easy

Problem:

Given an array of unique integers salary where salary[i] is the salary of the employee i.

Return the average salary of employees excluding the minimum and maximum salary.

Example:

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

Solution:

To solve this problem, we need to first find the minimum and maximum salary from the given array. Then we need to calculate the sum of the remaining salaries and divide it by the total number of employees excluding the two extreme salaries.

The steps to solve this problem are as follows:

- Initialize two variables to keep track of the minimum and maximum salary.
- Iterate over the given array and update the minimum and maximum salary if a smaller or larger value is found respectively.
- Calculate the sum of the remaining salaries by subtracting the minimum and maximum salary from the total sum of salaries.
- Divide the sum of salaries by the total number of employees excluding the two extreme salaries.
- Return the average salary.

Let's see the implementation of the above steps in code:

class Solution: def average(self, salary: List[int]) -> float:

```
# initialize minimum and maximum salary variables
min_salary = float('inf')
max_salary = float('-inf')
# find minimum and maximum salaries
for s in salary:
if s < min_salary:
min_salary = s
if s > max_salary:
max_salary = s
# calculate sum of remaining salaries
sum_salary = sum(salary) - min_salary - max_salary
# calculate average salary
avg_salary = sum_salary / (len(salary) - 2)
return avg_salary
```

Time Complexity:

The time complexity of the above solution is O(n), where n is the number of employees.

Space Complexity:

The space complexity of the above solution is O(1), as we are not using any extra space other than the two variables to keep track of the minimum and maximum salary.

## Average Salary Excluding The Minimum And Maximum Salary Solution Code

`1`