Average Salary Excluding the Minimum and Maximum Salary

🏠 ⬅️ ➡️

You are given an array of unique integers salary where salary[i] is the salary of the ith employee.

Return the average salary of employees excluding the minimum and maximum salary. Answers within 10-5 of the actual answer will be accepted.

Example 1:

Input: salary = [4000,3000,1000,2000] Output: 2500.00000 Explanation: Minimum salary and maximum salary are 1000 and 4000 respectively. Average salary excluding minimum and maximum salary is (2000+3000) / 2 = 2500

Example 2:

Input: salary = [1000,2000,3000] Output: 2000.00000 Explanation: Minimum salary and maximum salary are 1000 and 3000 respectively. Average salary excluding minimum and maximum salary is (2000) / 1 = 2000

Constraints:

  • 3 <= salary.length <= 100
  • 1000 <= salary[i] <= 106
  • All the integers of salary are unique.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer, parameter :: dp = selected_real_kind(15, 307)

    integer, dimension(:), allocatable :: salary
    real(dp) :: avg

    call get_array(salary)

    avg = average(salary)

    write (*, '(F8.5)') avg

contains

    subroutine get_array(arr)
        implicit none
        integer, dimension(:), allocatable, intent(out) :: arr
        integer :: i, n

        read *, n
        allocate(arr(n))

        do i = 1, n
            read *, arr(i)
        end do
    end subroutine get_array

    function average(arr) result(avg)
        implicit none
        integer, dimension(:), intent(in) :: arr
        real(dp) :: avg
        integer :: i, n

        n = size(arr)
        avg = sum(arr)
        do i = 1, n
            if (arr(i) == minval(arr)) then
                avg = avg - arr(i)
            else if (arr(i) == maxval(arr)) then
                avg = avg - arr(i)
            end if
        end do
        avg = avg / (n - 2)
    end function average

end program main
Compiled
Executed
Correct
PROGRAM AVERAGE_SALARY
      IMPLICIT NONE

      INTEGER, PARAMETER :: dp = SELECTED_REAL_KIND(12, 60)
      INTEGER :: n, i
      REAL(KIND=dp) :: salary(100), sum = 0.0_dp

      ! Read the number of salaries
      READ(*,*) n

      ! Read the salaries
      DO i = 1, n
         READ(*,*) salary(i)
      END DO

      ! Calculate the sum of the salaries excluding the minimum and maximum
      DO i = 2, n - 1
         sum = sum + salary(i)
      END DO

      ! Print the average salary
      WRITE(*,*) sum / (n - 2)

      END PROGRAM AVERAGE_SALARY
🌐 Data from online sources
def count_prefix_aligned(flips):
    count = 0
    for i in range(len(flips)):
        if flips[i] == i + 1:
            count += 1
    return count

The algorithm iterates through the given flips list (or array) and, at each step, checks if the value of flips[i] is equal to the 1-indexed position (i.e., i+1). If the condition is true, it means that the bit is flipped at the correct position to make the binary string prefix-aligned at that step, so we increment the counter. At the end of the loop, the counter represents the total number of prefix-aligned steps in the flipping process.

🌐 Data from online sources
int countPrefixAligned(const vector<int>& flips) {
    int count = 0;
    for (int i = 0; i < flips.size(); i++) {
        if (flips[i] == i + 1) {
            count++;
        }
    }
    return count;
}

The algorithm iterates through the given flips list (or array) and, at each step, checks if the value of flips[i] is equal to the 1-indexed position (i.e., i+1). If the condition is true, it means that the bit is flipped at the correct position to make the binary string prefix-aligned at that step, so we increment the counter. At the end of the loop, the counter represents the total number of prefix-aligned steps in the flipping process.