Choose n elements such that their mean is maximum | Utility function to print the contents of an array ; Function to print the array with maximum mean ; Sort the original array ; Construct new array ; Print the resultant array ; Driver code
function printArray ( arr , n ) { for ( i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } function printMaxMean ( arr , n ) { var newArr = Array ( n ) . fill ( 0 ) ; arr . sort ( ( a , b ) => a - b ) ; for ( i = 0 ; i < n ; i ++ ) newArr [ i ] = arr [ i + n ] ; printArray ( newArr , n ) ; } var arr = [ 4 , 8 , 3 , 1 , 3 , 7 , 0 , 4 ] ; var n = arr . length ; printMaxMean ( arr , n / 2 ) ;