Split array into K non-empty subsets such that sum of their maximums and minimums is maximized
Last Updated :
03 Apr, 2023
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 <bits/stdc++.h>
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
<script>
// JavaScript program for the above approach
// Function find maximum sum of
// minimum and maximum of K subsets
function maximumSum(arr, S, N, K)
{
// Stores the result
var ans = 0;
var i;
// Sort the array arr[] in
// decreasing order
arr.sort((a, b) => b - a);
// Traverse the range [0, K]
for (i = 0; i < K; i++)
ans += arr[i];
// Sort the array S[] in
// ascending order
S.sort();
// S.reverse();
// Traverse the array S[]
for (i = 0; i < K; i++) {
// 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
var counter = K - 1;
// Traverse the array S[]
for (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
var arr = [1, 13, 7, 17];
var S = [1, 3];
var N = arr.length;
var K = S.length;
document.write(maximumSum(arr, S, N, K));
</script>
Time Complexity: O(N log N)
Auxiliary Space: O(1)
Similar Reads
Split array into K subsets to maximize their sum of maximums and minimums Given an integer K and an array A[ ] whose length is multiple of K, the task is to split the elements of the given array into K subsets, each having an equal number of elements, such that the sum of the maximum and minimum elements of each subset is the maximum summation possible. Examples: Input: K
6 min read
Split array into K subarrays such that sum of maximum of all subarrays is maximized Given an array arr[] of size N and a number K, the task is to partition the given array into K contiguous subarrays such that the sum of the maximum of each subarray is the maximum possible. If it is possible to split the array in such a manner, then print the maximum possible sum. Otherwise, print
10 min read
Split Array into K non-overlapping subset such that maximum among all subset sum is minimum Given an array arr[] consisting of N integers and an integer K, the task is to split the given array into K non-overlapping subsets such that the maximum among the sum of all subsets is minimum. Examples: Input: arr[] = {1, 7, 9, 2, 12, 3, 3}, M = 3Output: 13Explanation:One possible way to split the
7 min read
Distribute given arrays into K sets such that total sum of maximum and minimum elements of all sets is maximum Given two arrays, the first arr[] of size N and the second brr[] of size K. The task is to divide the first array arr[] into K sets such that the i-th set should contain brr[i] elements from the second array brr[], and the total sum of maximum and minimum elements of all sets is maximum. Examples: I
8 min read
Divide the array in K segments such that the sum of minimums is maximized Given an array a of size N and an integer K, the task is to divide the array into K segments such that sum of the minimum of K segments is maximized. Examples: Input: a[] = {5, 7, 4, 2, 8, 1, 6}, K = 3 Output: 7 Divide the array at indexes 0 and 1. Then the segments are {5}, {7}, {4, 2, 8, 1, 6}. Su
9 min read