Advertisement

Average Salary Excluding the Minimum and Maximum Salary - LeetCode 1491 Solution

Average Salary Excluding the Minimum and Maximum Salary - Complete Solution Guide

Average Salary Excluding the Minimum and Maximum Salary is LeetCode problem 1491, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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. Example 1: 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 Example 2: Input: salary = [1000

Detailed Explanation

The problem asks you to calculate the average salary of employees, excluding the highest and lowest salaries. The input is an array of integers representing individual employee salaries. All salaries are unique, and the array will always contain at least three salaries. The output is a floating-point number representing the average salary after excluding the minimum and maximum values. The output should be accurate within 10<sup>-5</sup>.

Solution Approach

The provided solutions use a linear scan approach. They iterate through the salary array once to find the minimum and maximum salaries while simultaneously calculating the sum of all salaries. Afterward, the minimum and maximum salaries are subtracted from the total sum, and the result is divided by the number of employees minus two (to exclude the minimum and maximum). This ensures the average is calculated correctly excluding the extreme values.

Step-by-Step Algorithm

  1. Step 1: Initialize variables: `minSalary`, `maxSalary`, and `sum`. Initialize `minSalary` and `maxSalary` to the first element of the `salary` array. Initialize `sum` to 0.
  2. Step 2: Iterate through the `salary` array. For each salary:
  3. Step 2a: Update `minSalary` if the current salary is less than the current `minSalary`.
  4. Step 2b: Update `maxSalary` if the current salary is greater than the current `maxSalary`.
  5. Step 2c: Add the current salary to `sum`.
  6. Step 3: Subtract `minSalary` and `maxSalary` from `sum`.
  7. Step 4: Divide `sum` by `salary.length - 2` to get the average salary and return the result.

Key Insights

  • Insight 1: We need to find the minimum and maximum salaries efficiently to exclude them from the average calculation.
  • Insight 2: A single pass through the array is sufficient to find the minimum, maximum, and sum of salaries simultaneously, optimizing for time complexity.
  • Insight 3: Handling the case where there are only three salaries needs to be considered implicitly; the algorithm should gracefully handle this without needing special conditional logic.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array, Sorting.

Companies

Asked at: Netsuite.