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
def maxAverage ( A , N , X , Y ) : NEW_LINE INDENT A . sort ( ) NEW_LINE sum = 0 NEW_LINE res = 0 NEW_LINE count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += A [ i ] NEW_LINE count += 1 NEW_LINE if ( count == X ) : NEW_LINE INDENT if ( N - i - 1 < X ) : NEW_LINE INDENT i += 1 NEW_LINE cnt = 0 NEW_LINE while ( i < N ) : NEW_LINE INDENT cnt += 1 NEW_LINE sum += A [ i ] NEW_LINE i += 1 NEW_LINE DEDENT X = X + cnt NEW_LINE res += sum / X NEW_LINE break NEW_LINE DEDENT res += sum / X NEW_LINE sum = 0 NEW_LINE count = 0 NEW_LINE DEDENT DEDENT print ( res ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 4 , 10 , 6 , 5 ] NEW_LINE N = len ( A ) NEW_LINE X = 2 NEW_LINE Y = 3 NEW_LINE maxAverage ( A , N , X , Y ) NEW_LINE DEDENT