Average Salary Excluding the Minimum and Maximum Salary - Problem

You're working as a payroll analyst for a company and need to calculate the fair average salary for performance evaluation purposes.

Given an array of unique integers salary where salary[i] represents the salary of the i-th employee, you need to:

  • ๐Ÿšซ Exclude the minimum salary (outlier - might be intern/part-time)
  • ๐Ÿšซ Exclude the maximum salary (outlier - might be executive/exceptional case)
  • ๐Ÿ“Š Calculate the average of the remaining salaries

This approach helps eliminate extreme outliers that might skew the average compensation analysis.

Return: The average salary after removing the minimum and maximum values.

Note: Answers within 10-5 of the actual answer will be accepted.

Input & Output

example_1.py โ€” Basic Case
$ Input: salary = [4000,3000,1000,2000]
โ€บ Output: 2500.0
๐Ÿ’ก Note: Minimum salary is 1000, maximum salary is 4000. After excluding them, we have [3000, 2000]. Average = (3000 + 2000) / 2 = 2500.0
example_2.py โ€” Higher Salaries
$ Input: salary = [1000,2000,3000]
โ€บ Output: 2000.0
๐Ÿ’ก Note: Minimum is 1000, maximum is 3000. After exclusion: [2000]. Average = 2000 / 1 = 2000.0
example_3.py โ€” Larger Dataset
$ Input: salary = [6000,5000,4000,3000,2000,1000]
โ€บ Output: 3500.0
๐Ÿ’ก Note: Min=1000, Max=6000. Remaining: [5000,4000,3000,2000]. Average = (5000+4000+3000+2000) / 4 = 3500.0

Constraints

  • 3 โ‰ค salary.length โ‰ค 100
  • 1000 โ‰ค salary[i] โ‰ค 106
  • All the integers of salary are unique
  • Answers within 10-5 of the actual answer will be accepted

Visualization

Tap to expand
๐Ÿ“Š Salary Analysis: Excluding OutliersEmployee Salaries:$1,000InternโŒ MIN$2,000Juniorโœ… KEEP$3,000Mid-levelโœ… KEEP$4,000ExecutiveโŒ MAX๐Ÿ“ˆ Calculation Process:1. Total Sum: $1,000 + $2,000 + $3,000 + $4,000 = $10,0002. Remove Outliers: $10,000 - $1,000 (min) - $4,000 (max) = $5,0003. Valid Employees: 4 total - 2 outliers = 2 employees4. Fair Average: $5,000 รท 2 = $2,500๐Ÿ’ก This represents the typical salary range, excluding extreme cases
Understanding the Visualization
1
Data Collection
Gather all employee salary data into an array
2
Outlier Detection
Identify minimum and maximum salaries as outliers
3
Fair Average
Calculate average excluding the extreme values
Key Takeaway
๐ŸŽฏ Key Insight: By eliminating salary outliers (minimum and maximum), we get a more representative average that better reflects typical employee compensation levels.
Asked in
Amazon 35 Microsoft 28 Google 22 Apple 18
56.4K Views
Medium Frequency
~8 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen