# Leetcode – Average Salary Excluding the Minimum and Maximum Salary Solution

In the ever-expanding world of competitive programming and technical interviews, a variety of problems test various facets of a candidate’s coding skills. Among these are problems that require array manipulation and mathematical logic. One such problem is the “Average Salary Excluding the Minimum and Maximum Salary” problem on Leetcode. This problem may initially seem simple, but it offers an opportunity to delve into array handling, statistics, and mathematical operations.

## Problem Statement

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: The minimum salary is 1000 and the maximum salary is 4000. Therefore, the average salary excluding the two would be (3000 + 2000) / 2 = 2500.

## Basic Approach: Sorting the Array

The simplest approach to tackle this problem is to sort the array of salaries. Once the array is sorted, we can easily identify and remove the minimum and maximum elements and proceed to calculate the average of the remaining elements.

#### Python Code:

def average(salary):
salary.sort()
return sum(salary[1:-1]) / (len(salary) - 2)

# Test the function
print(average([4000, 3000, 1000, 2000]))  # Output should be 2500.0

### Time Complexity

Sorting the array takes O(n log ⁡n) time, while summing takes O(n), leading to a total time complexity of O(n log⁡ n).

### Space Complexity

Since the array is sorted in-place, the space complexity is O(1).

## Optimized Approach: One-pass Solution

An optimized approach involves going through the array just once to find the minimum, maximum, and the sum of all elements. Once these are obtained, calculating the average excluding the min and max values is straightforward.

#### Python Code:

def average(salary):
min_val, max_val, total = float('inf'), float('-inf'), 0

for sal in salary:
min_val = min(min_val, sal)
max_val = max(max_val, sal)
total += sal

return (total - min_val - max_val) / (len(salary) - 2)

# Test the function
print(average([4000, 3000, 1000, 2000]))  # Output should be 2500.0

### Time Complexity

This approach has a time complexity of O(n), which is more efficient than sorting the array.

### Space Complexity

The space complexity remains O(1) since we use only a constant amount of extra space.

## Edge Cases and Further Considerations

• Small Array Sizes: What if the array contains only two or three elements? By definition, the minimum and maximum would be excluded, which may lead to division by zero errors.
• Floating Point Accuracy: When dealing with salaries or any monetary value, the precision of floating-point operations can be a concern.
• Negative Salaries: The problem specifies that the salaries are unique integers, but it doesn’t clarify if they could be negative. A salary generally can’t be negative, but a thorough solution should consider this case.

## Conclusion

The “Average Salary Excluding the Minimum and Maximum Salary” problem serves as a useful exercise for array manipulation and mathematical calculations. We explored both basic and optimized approaches, considered their time and space complexities, and also touched on edge cases.