Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ff1dd23

Browse files
committed
[Optimized the performance by enhance for loop]: Have implemented the program to find the average salary excluding the minimum and maximum salary.
1 parent a00eda4 commit ff1dd23

File tree

2 files changed

+71
-22
lines changed

2 files changed

+71
-22
lines changed

.idea/workspace.xml

Lines changed: 22 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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

Comments
 (0)