text: Split array into K subsets to maximize their sum of maximums and minimums | C # program to implement the above approach ; Function that prints the maximum sum possible ; Find elements in each group ; Sort all elements in non - descending order ; Add K largest elements ; For sum of minimum elements from each subset ; Printing the maximum sum ; Driver code
code: using System ; class GFG { static void maximumSum ( int [ ] arr , int n , int k ) { int elt = n / k ; int sum = 0 ; Array . Sort ( arr ) ; int count = 0 ; int i = n - 1 ; while ( count < k ) { sum += arr [ i ] ; i -- ; count ++ ; } count = 0 ; i = 0 ; while ( count < k ) { sum += arr [ i ] ; i += elt - 1 ; count ++ ; } Console . WriteLine ( sum ) ; } public static void Main ( String [ ] args ) { int [ ] Arr = { 1 , 13 , 7 , 17 , 6 , 5 } ; int K = 2 ; int size = Arr . Length ; maximumSum ( Arr , size , K ) ; } }