Maximize sum of averages of subsequences of lengths lying in a given range | Java program for the above approach ; 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
import java . util . * ; class GFG { static void maxAverage ( int A [ ] , int N , int X , int Y ) { Arrays . sort ( A ) ; int sum = 0 ; double res = 0 ; int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += A [ i ] ; count ++ ; if ( count == X ) { if ( N - i - 1 < X ) { i ++ ; int cnt = 0 ; while ( i < N ) { cnt ++ ; sum += A [ i ] ; i ++ ; } X = X + cnt ; res += ( double ) sum / ( double ) ( X ) ; break ; } res += ( double ) sum / ( double ) ( X ) ; sum = 0 ; count = 0 ; } } System . out . println ( res ) ; } public static void main ( String [ ] args ) { int A [ ] = { 4 , 10 , 6 , 5 } ; int N = A . length ; int X = 2 , Y = 3 ; maxAverage ( A , N , X , Y ) ; } }