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
salary
are unique.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
At line 22 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7f00aaa52960 in ??? #1 0x7f00aaa534d9 in ??? #2 0x7f00aaca717b in ??? #3 0x7f00aaca0684 in ??? #4 0x7f00aaca12aa in ??? #5 0x574c94a875c0 in get_array.1 #6 0x574c94a874b3 in MAIN__ #7 0x574c94a8786d in main
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
At line 9 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x79114dcbd960 in ??? #1 0x79114dcbe4d9 in ??? #2 0x79114df1217b in ??? #3 0x79114df0b684 in ??? #4 0x79114df0c2aa in ??? #5 0x5841bf94e21a in MAIN__ #6 0x5841bf94e3d8 in main
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.
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.