|
| 1 | +/* |
| 2 | +Average Salary Excluding the Minimum and Maximum Salary |
| 3 | +Link: https://leetcode.com/problems/average-salary-excluding-the-minimum-and-maximum-salary/ |
| 4 | +
|
| 5 | +You are given an array of unique integers salary where salary[i] is the salary of the ith employee. |
| 6 | +
|
| 7 | +Return the average salary of employees excluding the minimum and maximum salary. Answers within 10-5 of the actual answer will be accepted. |
| 8 | +
|
| 9 | +Example 1: |
| 10 | +Input: salary = [4000,3000,1000,2000] |
| 11 | +Output: 2500.00000 |
| 12 | +Explanation: Minimum salary and maximum salary are 1000 and 4000 respectively. |
| 13 | +Average salary excluding minimum and maximum salary is (2000+3000) / 2 = 2500 |
| 14 | +
|
| 15 | +Example 2: |
| 16 | +Input: salary = [1000,2000,3000] |
| 17 | +Output: 2000.00000 |
| 18 | +Explanation: Minimum salary and maximum salary are 1000 and 3000 respectively. |
| 19 | +Average salary excluding minimum and maximum salary is (2000) / 1 = 2000 |
| 20 | +
|
| 21 | +Constraints: |
| 22 | +3 <= salary.length <= 100 |
| 23 | +1000 <= salary[i] <= 106 |
| 24 | +All the integers of salary are unique. |
| 25 | + */ |
| 26 | +package com.raj; |
| 27 | + |
| 28 | +public class AverageSalaryExcludingTheMinimumAndMaximumSalary { |
| 29 | + public static void main(String[] args) { |
| 30 | + // Initialization. |
| 31 | + int[] salary = {4000, 3000, 1000, 2000}; |
| 32 | + int min = 1_000_000; |
| 33 | + int max = 1000; |
| 34 | + double ans = 0; |
| 35 | + |
| 36 | + // Logic. |
| 37 | + for (int i : salary) { |
| 38 | + ans += i; |
| 39 | + min = Math.min(min, i); |
| 40 | + max = Math.max(max, i); |
| 41 | + } |
| 42 | + |
| 43 | + // Set the average of salary with length - 2 (excluding the minimum and maximum salary). |
| 44 | + double result = (ans - min - max) / (salary.length - 2); |
| 45 | + |
| 46 | + // Display the result. |
| 47 | + System.out.println(result + " is the average salary excluding the minimum and maximum salary."); |
| 48 | + } |
| 49 | +} |
0 commit comments