Average Salary Excluding the Minimum and Maximum Salary

Given an array of unique integers 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 = { 4000300010002000 };
        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 = 1i < n - 1i++)
        d += salary[i];
    return d / (n - 2);
}

int main()
{
    vector<intsalary = {4000300010002000};
    cout << average(salary);
    return 0;
}


No comments:

Post a Comment