Given an array of unique integers
Return the average salary of employees excluding the minimum and maximum salary.
salary
where salary[i]
is the salary of the employee i
.Return the average salary of employees excluding the minimum and maximum salary.
Example 1:
Input: salary = [4000,3000,1000,2000]
Output: 2500.00000
Approach
Java
import java.util.Arrays;public class AvgSalaryExcluding {public static void main(String[] args) {int[] salary = { 4000, 3000, 1000, 2000 };System.out.println(average(salary));}static double average(int[] salary) {Arrays.sort(salary);double d = 0;int n = salary.length;for (int i = 1; i < n - 1; i++)d += salary[i];return d / (n - 2);}}
C++
#include <bits/stdc++.h>using namespace std;double average(vector<int> &salary){sort(salary.begin(), salary.end());double d = 0;int n = salary.size();for (int i = 1; i < n - 1; i++)d += salary[i];return d / (n - 2);}int main(){vector<int> salary = {4000, 3000, 1000, 2000};cout << average(salary);return 0;}
No comments:
Post a Comment