Average of remaining elements after removing K largest and K smallest elements from array | Function to find average ; Base case if 2 * k >= n means all element get removed ; First sort all elements ; Sum of req number ; Find average ; Driver code
function average ( arr , n , k ) { var total = 0 ; if ( 2 * k >= n ) return 0 ; arr . sort ( ) ; var start = k , end = n - k - 1 ; for ( i = start ; i <= end ; i ++ ) total += arr [ i ] ; return ( total / ( n - 2 * k ) ) ; } var arr = [ 1 , 2 , 4 , 4 , 5 , 6 ] ; var n = arr . length ; var k = 2 ; document . write ( average ( arr , n , k ) ) ;