Write a Java Program to find the average salary of employees excluding the minimum and maximum salary. Assume an array of unique integers salary where salary[i] is the salary of the ith employee.
In the problem ”Average Salary Excluding the Minimum and Maximum Salary” we are given a salary array. where each element in the array represents the salary of different employees. Each value in the array is unique.
Our task is to calculate the average salary of the employee excluding the minimum and the maximum salary.
Explanation:
In the let the salary array 9000 is the maximum salary and 1000 is the minimum salary. As we need to find the average salary excluding the minimum and maximum salary so we will not add these values to the total sum. The total sum is 19000 so the average is 19000/4 that is 4750.
let salary |
9000 | 8000 | 2000 | 3000 | 6000 |1000 |
maximum salary minimum salary |
sum of all salary=29000 |
total salary Excluding the Minimum and Maximum salary=29000-9000-1000=19000 |
Average Salary Excluding the Minimum and Maximum Salary=19000/4=4750 |
Approach
This is a basic math problem. Our task is to find the minimum and the maximum salary then find the average of the remaining salary. We will follow these steps:
- Initialize minimum salary with INT_MAX, the maximum salary with INT_MIN. We will use a variable to store the salary sum so initialize it with 0.
- Traverse the array and add each salary to the sum. Meanwhile, also update the value of the minimum salary and maximum salary.
- If the value of n is smaller than three then the average salary will be zero else average salary will be (total salary-the minimum salary-the maximum salary)/(n-2).
- We will multiply (n-2) with 1.0 because the average salary can be a double value.
Step by step
Solved in 2 steps