Split Array into K non-overlapping subset such that maximum among all subset sum is minimum
Last Updated :
22 Feb, 2023
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 = 3
Output: 13
Explanation:
One possible way to split the array into 3 non-overlapping subsets is {arr[4], arr[0]}, {arr[2], arr[6]}, and {arr[1], arr[5], arr[3]}.
The sum of each subset is 13, 12 and 12 respectively. Now, the maximum among all the sum of subsets is 13, which is the minimum possible sum.
Input: arr[] = {1, 2, 3, 4, 5}, M = 2
Output: 8
Approach: The given problem can be solved by the Greedy Approach by using the priority queue and sorting the given array. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
+++++
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to split the array into M
// groups such that maximum of the sum
// of all elements of all the groups
// is minimized
int findMinimumValue(int arr[], int N,
int M)
{
// Sort the array in decreasing order
sort(arr, arr + N, greater<int>());
// Initialize priority queue (Min heap)
priority_queue<int, vector<int>,
greater<int> >
pq;
// Push 0 for all the M groups
for (int i = 1; i <= M; ++i) {
pq.push(0);
}
// Traverse the array, arr[]
for (int i = 0; i < N; ++i) {
// Pop the group having the
// minimum sum
int val = pq.top();
pq.pop();
// Increment val by arr[i]
val += arr[i];
// Push the new sum of the
// group into the pq
pq.push(val);
}
// Iterate while size of the pq
// is greater than 1
while (pq.size() > 1) {
pq.pop();
}
// Return result
return pq.top();
}
// Driver Code
int main()
{
int arr[] = { 1, 7, 9, 2, 12, 3, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 3;
cout << findMinimumValue(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class Main
{
// Function to split the array into M
// groups such that maximum of the sum
// of all elements of all the groups
// is minimized
static int findMinimumValue(Vector<Integer> arr, int N, int M)
{
// Sort the array in decreasing order
Collections.sort(arr);
Collections.reverse(arr);
// Initialize priority queue (Min heap)
Vector<Integer> pq = new Vector<Integer>();
// Push 0 for all the M groups
for (int i = 1; i <= M; ++i) {
pq.add(0);
}
Collections.sort(pq);
// Traverse the array, arr[]
for (int i = 0; i < N; ++i) {
// Pop the group having the
// minimum sum
int val = pq.get(0);
pq.remove(0);
// Increment val by arr[i]
val += arr.get(i);
// Push the new sum of the
// group into the pq
pq.add(val);
Collections.sort(pq);
}
// Iterate while size of the pq
// is greater than 1
while (pq.size() > 1) {
pq.remove(0);
}
// Return result
return pq.get(0);
}
public static void main(String[] args) {
Integer[] arr = { 1, 7, 9, 2, 12, 3, 3 };
Vector<Integer> Arr = new Vector<Integer>();
Collections.addAll(Arr, arr);
int N = Arr.size();
int K = 3;
System.out.println(findMinimumValue(Arr, N, K));
}
}
// This code is contributed by divyesh072019.
Python3
# Python3 program for the above approach
# Function to split the array into M
# groups such that maximum of the sum
# of all elements of all the groups
# is minimized
def findMinimumValue(arr, N, M):
# Sort the array in decreasing order
arr.sort()
arr.reverse()
# Initialize priority queue (Min heap)
pq = []
# Push 0 for all the M groups
for i in range(1, M + 1):
pq.append(0)
pq.sort()
# Traverse the array, arr[]
for i in range(N):
# Pop the group having the
# minimum sum
val = pq[0]
del pq[0]
# Increment val by arr[i]
val += arr[i]
# Push the new sum of the
# group into the pq
pq.append(val)
pq.sort()
# Iterate while size of the pq
# is greater than 1
while (len(pq) > 1) :
del pq[0]
# Return result
return pq[0]
arr = [ 1, 7, 9, 2, 12, 3, 3 ]
N = len(arr)
K = 3
print(findMinimumValue(arr, N, K))
# This code is contributed by suresh07.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to split the array into M
// groups such that maximum of the sum
// of all elements of all the groups
// is minimized
static int findMinimumValue(int[] arr, int N, int M)
{
// Sort the array in decreasing order
Array.Sort(arr);
Array.Reverse(arr);
// Initialize priority queue (Min heap)
List<int> pq = new List<int>();
// Push 0 for all the M groups
for (int i = 1; i <= M; ++i) {
pq.Add(0);
}
pq.Sort();
// Traverse the array, arr[]
for (int i = 0; i < N; ++i) {
// Pop the group having the
// minimum sum
int val = pq[0];
pq.RemoveAt(0);
// Increment val by arr[i]
val += arr[i];
// Push the new sum of the
// group into the pq
pq.Add(val);
pq.Sort();
}
// Iterate while size of the pq
// is greater than 1
while (pq.Count > 1) {
pq.RemoveAt(0);
}
// Return result
return pq[0];
}
static void Main() {
int[] arr = { 1, 7, 9, 2, 12, 3, 3 };
int N = arr.Length;
int K = 3;
Console.Write(findMinimumValue(arr, N, K));
}
}
// This code is contributed by divyeshrabadiya07.
JavaScript
<script>
// Javascript program for the above approach
// Function to split the array into M
// groups such that maximum of the sum
// of all elements of all the groups
// is minimized
function findMinimumValue(arr, N, M)
{
// Sort the array in decreasing order
arr.sort(function(a, b){return a - b});
arr.reverse();
// Initialize priority queue (Min heap)
let pq = [];
// Push 0 for all the M groups
for (let i = 1; i <= M; ++i) {
pq.push(0);
}
pq.sort(function(a, b){return a - b});
// Traverse the array, arr[]
for (let i = 0; i < N; ++i) {
// Pop the group having the
// minimum sum
let val = pq[0];
pq.shift();
// Increment val by arr[i]
val += arr[i];
// Push the new sum of the
// group into the pq
pq.push(val);
pq.sort(function(a, b){return a - b});
}
// Iterate while size of the pq
// is greater than 1
while (pq.length > 1) {
pq.shift();
}
// Return result
return pq[0];
}
let arr = [ 1, 7, 9, 2, 12, 3, 3 ];
let N = arr.length;
let K = 3;
document.write(findMinimumValue(arr, N, K));
// This code is contributed by decode2207.
</script>
Time Complexity: O(N*log K)
Auxiliary Space: O(M)
Similar Reads
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-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
7 min read
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
Maximum number of subsets an array can be split into such that product of their minimums with size of subsets is at least K Given an array arr[] consisting of N integers and an integer K, the task is to find the maximum number of disjoint subsets that the given array can be split into such that the product of the minimum element of each subset with the size of the subset is at least K. Examples: Input: arr[] = {7, 11, 2,
8 min read
Partition a set into two non-empty subsets such that the difference of subset sums is maximum Given a set of integers S, the task is to divide the given set into two non-empty sets S1 and S2 such that the absolute difference between their sums is maximum, i.e., abs(sum(S1) â sum(S2)) is maximum. Example: Input: S[] = { 1, 2, 1 } Output: 2Explanation:The subsets are {1} and {2, 1}. Their abso
7 min read