Leetcode 1491: Average Salary Excluding the Minimum and Maximum Salary

grid47
grid47
Exploring patterns and algorithms
Jun 10, 2024 5 min read

You are given an array salary where each element represents the salary of an employee. The salaries are all unique. Return the average salary of the employees, excluding the minimum and maximum salary. Your answer should be correct to an absolute error of no more than 10^-5.
Problem
Approach
Steps
Complexity
Input: You are given an array of unique integers, where each integer represents the salary of an employee. The array length will be at least 3.
Example: salary = [5000, 3000, 1500, 3500]
Constraints:
• 3 <= salary.length <= 100
• 1000 <= salary[i] <= 10^6
• All salary values are unique.
Output: Return the average salary, excluding the minimum and maximum salaries in the list. The result should be correct up to an absolute error of no more than 10^-5.
Example: Output: 3000.00000
Constraints:
• The output should be a floating-point value.
Goal: Calculate the average of all salaries, excluding the smallest and largest salaries.
Steps:
• Identify the minimum and maximum salaries from the array.
• Sum the remaining salaries (excluding the minimum and maximum).
• Divide the sum by the number of remaining salaries to get the average.
Goal: The input array contains at least 3 elements and all salary values are unique.
Steps:
• The array size is between 3 and 100.
• Each salary value is between 1000 and 10^6.
Assumptions:
• The salary array is guaranteed to have at least 3 unique elements.
• The solution should avoid using any complex operations that will affect performance.
Input: salary = [5000, 3000, 1500, 3500]
Explanation: The minimum salary is 1500, and the maximum salary is 5000. Excluding these, the average of 3000 and 3500 is 3250.

Input: salary = [1200, 2200, 3200]
Explanation: The minimum salary is 1200, and the maximum salary is 3200. The only remaining salary is 2200.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus