Split array into equal length subsets with maximum sum of Kth largest element of each subset
Last Updated :
21 Apr, 2021
Given an array arr[] of size N, two positive integers M and K, the task is to partition the array into M equal length subsets such that the sum of the Kth largest element of all these subsets is maximum. If it is not possible to partition the array into M equal length subsets, then print -1.
Examples:
Input: arr[] = { 1, 2, 3, 4, 5, 6 }, M = 2, K = 2
Output: 8
Explanation:
Length of each subset = (N / M) = 3.
Possible subsets from the array are: { {1, 3, 4}, {2, 5, 6} }
Therefore, the sum of Kth largest element from each subset = 3 + 5 = 8
Input: arr[] = {11, 20, 2, 17}, M = 4, K = 1
Output: 50
Approach: The problem can be solved using Greedy technique. Follow the steps below to solve the problem:
- If N is not divisible M, then print -1.
- Initialize a variable, say maxSum, to store the maximum possible sum of Kth largest element of all the subsets of the array by partitioning the array into M equal length subset.
- Sort the array in descending order.
- Iterate over the range [1, M] using variable i and update maxSum += arr[i * K – 1].
- Finally, print the value of maxSum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maximumKthLargestsumPart( int arr[], int N,
int M, int K)
{
int maxSum = 0;
if (N % M)
return -1;
int sz = (N / M);
if (K > sz)
return -1;
sort(arr, arr + N, greater< int >());
for ( int i = 1; i <= M;
i++) {
maxSum
+= arr[i * K - 1];
}
return maxSum;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int M = 2;
int K = 1;
int N = sizeof (arr) / sizeof (arr[0]);
cout << maximumKthLargestsumPart(arr, N, M, K);
return 0;
}
|
Java
import java.util.*;
import java.util.Collections;
class GFG{
static int maximumKthLargestsumPart( int [] arr, int N,
int M, int K)
{
int maxSum = 0 ;
if (N % M != 0 )
return - 1 ;
int sz = (N / M);
if (K > sz)
return - 1 ;
Arrays.sort(arr);
int i, k, t;
for (i = 0 ; i < N / 2 ; i++)
{
t = arr[i];
arr[i] = arr[N - i - 1 ];
arr[N - i - 1 ] = t;
}
for (i = 1 ; i <= M; i++)
{
maxSum += arr[i * K - 1 ];
}
return maxSum;
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 , 5 , 6 };
int M = 2 ;
int K = 1 ;
int N = arr.length;
System.out.println(maximumKthLargestsumPart(
arr, N, M, K));
}
}
|
Python3
def maximumKthLargestsumPart(arr, N, M, K):
maxSum = 0
if (N % M ! = 0 ):
return - 1
sz = (N / M)
if (K > sz):
return - 1
arr = sorted (arr)
for i in range ( 0 , N / / 2 ):
t = arr[i]
arr[i] = arr[N - i - 1 ]
arr[N - i - 1 ] = t
for i in range ( 1 , M + 1 ):
maxSum + = arr[i * K - 1 ]
return maxSum
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 , 5 , 6 ]
M = 2
K = 1
N = len (arr)
print (maximumKthLargestsumPart(arr, N, M, K))
|
C#
using System;
class GFG{
static int maximumKthLargestsumPart( int [] arr, int N,
int M, int K)
{
int maxSum = 0;
if (N % M != 0)
return -1;
int sz = (N / M);
if (K > sz)
return -1;
Array.Sort(arr);
Array.Reverse(arr);
for ( int i = 1; i <= M; i++)
{
maxSum += arr[i * K - 1];
}
return maxSum;
}
static void Main()
{
int [] arr = { 1, 2, 3, 4, 5, 6 };
int M = 2;
int K = 1;
int N = arr.Length;
Console.WriteLine(maximumKthLargestsumPart(
arr, N, M, K));
}
}
|
Javascript
<script>
function maximumKthLargestsumPart(arr, N,
M, K)
{
let maxSum = 0;
if (N % M != 0)
return -1;
let sz = (N / M);
if (K > sz)
return -1;
arr.sort();
let i, k, t;
for (i = 0; i < N / 2; i++)
{
t = arr[i];
arr[i] = arr[N - i - 1];
arr[N - i - 1] = t;
}
for (i = 1; i <= M; i++)
{
maxSum += arr[i * K - 1];
}
return maxSum;
}
let arr = [ 1, 2, 3, 4, 5, 6 ];
let M = 2;
let K = 1;
let N = arr.length;
document.write(maximumKthLargestsumPart(
arr, N, M, K));
</script>
|
Time complexity: O(N * log(N))
Auxiliary space: O(1)
Share your thoughts in the comments
Please Login to comment...