Mean of given array after removal of K percent of smallest and largest array elements | Function to calculate the mean of a given array after removal of Kth percent of smallest and largest array elements ; Sort the array ; Find the K - th percent of the array size ; Traverse the array ; Skip the first K - th percent & last K - th percent array elements ; Mean of the rest of elements ; Print mean upto 5 decimal places ; Driver Code
function meanOfRemainingElements ( arr , N , K ) { arr . sort ( function ( a , b ) { return a - b } ) ; let kthPercent = Math . floor ( ( N * K ) / 100 ) ; let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) if ( i >= kthPercent && i < ( N - kthPercent ) ) sum += arr [ i ] ; let mean = sum / ( N - 2 * kthPercent ) ; document . write ( mean . toFixed ( 5 ) ) ; } let arr = [ 6 , 2 , 7 , 5 , 1 , 2 , 0 , 3 , 10 , 2 , 5 , 0 , 5 , 5 , 0 , 8 , 7 , 6 , 8 , 0 ] ; let arr_size = arr . length ; let K = 5 ; meanOfRemainingElements ( arr , arr_size , K ) ;