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++
#include <bits/stdc++.h>
using namespace std;
int findMinimumValue( int arr[], int N,
int M)
{
sort(arr, arr + N, greater< int >());
priority_queue< int , vector< int >,
greater< int > >
pq;
for ( int i = 1; i <= M; ++i) {
pq.push(0);
}
for ( int i = 0; i < N; ++i) {
int val = pq.top();
pq.pop();
val += arr[i];
pq.push(val);
}
while (pq.size() > 1) {
pq.pop();
}
return pq.top();
}
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
import java.util.*;
public class Main
{
static int findMinimumValue(Vector<Integer> arr, int N, int M)
{
Collections.sort(arr);
Collections.reverse(arr);
Vector<Integer> pq = new Vector<Integer>();
for ( int i = 1 ; i <= M; ++i) {
pq.add( 0 );
}
Collections.sort(pq);
for ( int i = 0 ; i < N; ++i) {
int val = pq.get( 0 );
pq.remove( 0 );
val += arr.get(i);
pq.add(val);
Collections.sort(pq);
}
while (pq.size() > 1 ) {
pq.remove( 0 );
}
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));
}
}
|
Python3
def findMinimumValue(arr, N, M):
arr.sort()
arr.reverse()
pq = []
for i in range ( 1 , M + 1 ):
pq.append( 0 )
pq.sort()
for i in range (N):
val = pq[ 0 ]
del pq[ 0 ]
val + = arr[i]
pq.append(val)
pq.sort()
while ( len (pq) > 1 ) :
del pq[ 0 ]
return pq[ 0 ]
arr = [ 1 , 7 , 9 , 2 , 12 , 3 , 3 ]
N = len (arr)
K = 3
print (findMinimumValue(arr, N, K))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int findMinimumValue( int [] arr, int N, int M)
{
Array.Sort(arr);
Array.Reverse(arr);
List< int > pq = new List< int >();
for ( int i = 1; i <= M; ++i) {
pq.Add(0);
}
pq.Sort();
for ( int i = 0; i < N; ++i) {
int val = pq[0];
pq.RemoveAt(0);
val += arr[i];
pq.Add(val);
pq.Sort();
}
while (pq.Count > 1) {
pq.RemoveAt(0);
}
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));
}
}
|
Javascript
<script>
function findMinimumValue(arr, N, M)
{
arr.sort( function (a, b){ return a - b});
arr.reverse();
let pq = [];
for (let i = 1; i <= M; ++i) {
pq.push(0);
}
pq.sort( function (a, b){ return a - b});
for (let i = 0; i < N; ++i) {
let val = pq[0];
pq.shift();
val += arr[i];
pq.push(val);
pq.sort( function (a, b){ return a - b});
}
while (pq.length > 1) {
pq.shift();
}
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));
</script>
|
Time Complexity: O(N*log K)
Auxiliary Space: O(M)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...