Average Salary Excluding the Minimum and Maximum Salary - Problem

You are given an array of unique integers salary where salary[i] is the salary of the i-th employee.

Return the average salary of employees excluding the minimum and maximum salary. Answers within 10^-5 of the actual answer will be accepted.

Input & Output

Example 1 — Basic Case
$ Input: salary = [4000,3000,1000,2000]
Output: 2500.0
💡 Note: Minimum salary is 1000, maximum salary is 4000. Average of remaining salaries: (3000 + 2000) / 2 = 2500.0
Example 2 — Minimum Size
$ Input: salary = [1000,2000,3000]
Output: 2000.0
💡 Note: Minimum salary is 1000, maximum salary is 3000. Average of remaining salaries: 2000 / 1 = 2000.0
Example 3 — Larger Array
$ Input: salary = [8000,9000,2000,3000,6000,1000]
Output: 4250.0
💡 Note: Minimum salary is 1000, maximum salary is 9000. Average of remaining: (8000+2000+3000+6000) / 4 = 4250.0

Constraints

  • 3 ≤ salary.length ≤ 100
  • 1000 ≤ salary[i] ≤ 106
  • All the integers of salary are unique

Visualization

Tap to expand
Average Salary Excluding Min and MaxInput: [4000, 3000, 1000, 2000]4000300010002000MAXINCLUDEMININCLUDEExclude Min (1000) and Max (4000)Remaining: [3000, 2000]Average = (3000 + 2000) / 2 = 2500.0Output: 2500.0
Understanding the Visualization
1
Input
Array of unique salary values
2
Process
Identify and exclude minimum and maximum salaries
3
Output
Average of remaining salaries
Key Takeaway
🎯 Key Insight: We only need to find min and max values to exclude them, no need to sort the entire array
Asked in
Apple 15 Microsoft 12
66.7K Views
Medium Frequency
~15 min Avg. Time
1.9K 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