GeeksforGeeks App
Open App
Browser
Continue

# Split array into K non-empty subsets such that sum of their maximums and minimums is maximized

Given two arrays arr[] and S[] consisting of N and K integers, the task is to find the maximum sum of minimum and maximum of each subset after splitting the array into K subsets such that the size of each subset is equal to one of the elements in the array S[].

Examples:

Input: arr[] = {1, 13, 7, 17}, S[] = {1, 3}
Output: 48
Explanation: Consider splitting the array as {17}, {1, 7, 13}, such that the size of subsets are 1 and 3 respectively, which is present in the array S[].
The sum of the maximum and minimum of each subset is (17 + 17 + 1 + 13) = 48.

Input: arr[ ] = {5, 1, -30, 0, 11, 20, 19}, S[] = {4, 1, 2}
Output: 45

Approach: The given problem can be solved using the Greedy Approach, the idea is to insert the first K maximum elements in each group and then start filling the smaller-sized groups first with greater elements. Follow the steps below to solve the problem:

• Initialize a variable, say ans as 0, to store the sum of the maximum and the minimum of all the subsets.
• Sort the array arr[] in descending order.
• Sort the array S[] in ascending order.
• Find the sum of the first K elements of the array and store it in the variable ans, and decrement all elements of the S[] by 1.
• Initialize a variable, say counter as K – 1, to store the index of the minimum element of each subset.
• Traverse the array S[] and increment counter by S[i] and add the value of arr[counter] to the ans.
• After completing the above steps, print the values of ans as the result.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function find maximum sum of``// minimum and maximum of K subsets``int` `maximumSum(``int` `arr[], ``int` `S[],``               ``int` `N, ``int` `K)``{``    ``// Stores the result``    ``int` `ans = 0;` `    ``// Sort the array arr[] in``    ``// decreasing order``    ``sort(arr, arr + N, greater<``int``>());` `    ``// Traverse the range [0, K]``    ``for` `(``int` `i = 0; i < K; i++)``        ``ans += arr[i];` `    ``// Sort the array S[] in``    ``// ascending order``    ``sort(S, S + K);` `    ``// Traverse the array S[]``    ``for` `(``int` `i = 0; i < K; i++) {` `        ``// If S[i] is 1``        ``if` `(S[i] == 1)``            ``ans += arr[i];` `        ``S[i]--;``    ``}` `    ``// Stores the index of the minimum``    ``// element of the i-th subset``    ``int` `counter = K - 1;` `    ``// Traverse the array S[]``    ``for` `(``int` `i = 0; i < K; i++) {` `        ``// Update the counter``        ``counter = counter + S[i];` `        ``if` `(S[i] != 0)``            ``ans += arr[counter];``    ``}` `    ``// Return the resultant sum``    ``return` `ans;``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 1, 13, 7, 17 };``    ``int` `S[] = { 1, 3 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);``    ``int` `K = ``sizeof``(S) / ``sizeof``(S[0]);``    ``cout << maximumSum(arr, S, N, K);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.io.*;``import` `java.lang.*;``import` `java.util.*;` `class` `GFG{` `// Function find maximum sum of``// minimum and maximum of K subsets``static` `int` `maximumSum(``int` `arr[], ``int` `S[],``                      ``int` `N, ``int` `K)``{``    ` `    ``// Stores the result``    ``int` `ans = ``0``;` `    ``// Sort the array arr[] in``    ``// decreasing order``    ``Arrays.sort(arr);``    ``for``(``int` `i = ``0``; i < N / ``2``; i++)``    ``{``        ``int` `temp = arr[i];``        ``arr[i] = arr[N - ``1` `- i];``        ``arr[N - ``1` `- i] = temp;``    ``}` `    ``// Traverse the range [0, K]``    ``for``(``int` `i = ``0``; i < K; i++)``        ``ans += arr[i];` `    ``// Sort the array S[] in``    ``// ascending order``    ``Arrays.sort(S);` `    ``// Traverse the array S[]``    ``for``(``int` `i = ``0``; i < K; i++)``    ``{``        ` `        ``// If S[i] is 1``        ``if` `(S[i] == ``1``)``            ``ans += arr[i];` `        ``S[i]--;``    ``}` `    ``// Stores the index of the minimum``    ``// element of the i-th subset``    ``int` `counter = K - ``1``;` `    ``// Traverse the array S[]``    ``for``(``int` `i = ``0``; i < K; i++)``    ``{` `        ``// Update the counter``        ``counter = counter + S[i];` `        ``if` `(S[i] != ``0``)``            ``ans += arr[counter];``    ``}` `    ``// Return the resultant sum``    ``return` `ans;``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `arr[] = { ``1``, ``13``, ``7``, ``17` `};``    ``int` `S[] = { ``1``, ``3` `};``    ``int` `N = arr.length;``    ``int` `K = S.length;``    ` `    ``System.out.println(maximumSum(arr, S, N, K));``}``}` `// This code is contributed by Kingash`

## Python3

 `# Python3 program for the above approach` `# Function find maximum sum of``# minimum and maximum of K subsets``def` `maximumSum(arr, S, N, K):``    ` `    ``# Stores the result``    ``ans ``=` `0` `    ``# Sort the array arr[] in``    ``# decreasing order``    ``arr ``=` `sorted``(arr)[::``-``1``]` `    ``# Traverse the range [0, K]``    ``for` `i ``in` `range``(K):``        ``ans ``+``=` `arr[i]` `    ``# Sort the array S[] in``    ``# ascending order``    ``S ``=` `sorted``(S)` `    ``# Traverse the array S[]``    ``for` `i ``in` `range``(K):``        ` `        ``# If S[i] is 1``        ``if` `(S[i] ``=``=` `1``):``            ``ans ``+``=` `arr[i]` `        ``S[i] ``-``=` `1` `    ``# Stores the index of the minimum``    ``# element of the i-th subset``    ``counter ``=` `K ``-` `1` `    ``# Traverse the array S[]``    ``for` `i ``in` `range``(K):``        ` `        ``# Update the counter``        ``counter ``=` `counter ``+` `S[i]` `        ``if` `(S[i] !``=` `0``):``            ``ans ``+``=` `arr[counter]` `    ``# Return the resultant sum``    ``return` `ans` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``arr ``=` `[ ``1``, ``13``, ``7``, ``17` `]``    ``S ``=` `[ ``1``, ``3` `]``    ``N ``=` `len``(arr)``    ``K ``=` `len``(S)``    ` `    ``print` `(maximumSum(arr, S, N, K))` `# This code is contributed by mohit kumar 29`

## C#

 `// C# program for the above approach``using` `System;``class` `GFG {` `    ``// Function find maximum sum of``    ``// minimum and maximum of K subsets``    ``static` `int` `maximumSum(``int``[] arr, ``int``[] S, ``int` `N, ``int` `K)``    ``{` `        ``// Stores the result``        ``int` `ans = 0;` `        ``// Sort the array arr[] in``        ``// decreasing order``        ``Array.Sort(arr);``        ``for` `(``int` `i = 0; i < N / 2; i++) {``            ``int` `temp = arr[i];``            ``arr[i] = arr[N - 1 - i];``            ``arr[N - 1 - i] = temp;``        ``}` `        ``// Traverse the range [0, K]``        ``for` `(``int` `i = 0; i < K; i++)``            ``ans += arr[i];` `        ``// Sort the array S[] in``        ``// ascending order``        ``Array.Sort(S);` `        ``// Traverse the array S[]``        ``for` `(``int` `i = 0; i < K; i++) {` `            ``// If S[i] is 1``            ``if` `(S[i] == 1)``                ``ans += arr[i];` `            ``S[i]--;``        ``}` `        ``// Stores the index of the minimum``        ``// element of the i-th subset``        ``int` `counter = K - 1;` `        ``// Traverse the array S[]``        ``for` `(``int` `i = 0; i < K; i++) {` `            ``// Update the counter``            ``counter = counter + S[i];` `            ``if` `(S[i] != 0)``                ``ans += arr[counter];``        ``}` `        ``// Return the resultant sum``        ``return` `ans;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main(``string``[] args)``    ``{``        ``int``[] arr = { 1, 13, 7, 17 };``        ``int``[] S = { 1, 3 };``        ``int` `N = arr.Length;``        ``int` `K = S.Length;` `        ``Console.WriteLine(maximumSum(arr, S, N, K));``    ``}``}` `// This code is contributed by ukasp.`

## Javascript

 ``

Output:

`48`

Time Complexity: O(N log N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up