Maximize sum of averages of subsequences of lengths lying in a given range | Function to find the maximum sum of average of groups ; Sort the given array ; Stores the sum of averages ; Stores count of array element ; Add the current value to the variable sum ; Increment the count by 1 ; If the current size is X ; If the remaining elements can 't become a group ; Iterate until i is less than N ; Update the value of X ; Update the average ; Find the average ; Reset the sum and count ; Print maximum sum of averages ; Driver Code
function maxAverage ( A , N , X , Y ) { A . sort ( function ( a , b ) { return a - b ; } ) let sum = 0 ; let res = 0 ; let count = 0 ; for ( let i = 0 ; i < N ; i ++ ) { sum += A [ i ] ; count ++ ; if ( count == X ) { if ( N - i - 1 < X ) { i ++ ; let cnt = 0 ; while ( i < N ) { cnt ++ ; sum += A [ i ] ; i ++ ; } X = X + cnt ; res += sum / X ; break ; } res += sum / X ; sum = 0 ; count = 0 ; } } document . write ( res . toPrecision ( 3 ) ) } let A = [ 4 , 10 , 6 , 5 ] ; let N = A . length ; let X = 2 , Y = 3 ; maxAverage ( A , N , X , Y ) ;