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++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the maximum sum of Kth
// largest element of M equal length partition
int maximumKthLargestsumPart(int arr[], int N,
int M, int K)
{
// Stores sum of K_th largest element
// of equal length partitions
int maxSum = 0;
// If N is not
// divisible by M
if (N % M)
return -1;
// Stores length of
// each partition
int sz = (N / M);
// If K is greater than
// length of partition
if (K > sz)
return -1;
// Sort array in
// descending porder
sort(arr, arr + N, greater<int>());
// Traverse the array
for (int i = 1; i <= M;
i++) {
// Update maxSum
maxSum
+= arr[i * K - 1];
}
return maxSum;
}
// Driver Code
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
// Java program to implement
// the above approach
import java.util.*;
import java.util.Collections;
class GFG{
// Function to find the maximum sum of Kth
// largest element of M equal length partition
static int maximumKthLargestsumPart(int[] arr, int N,
int M, int K)
{
// Stores sum of K_th largest element
// of equal length partitions
int maxSum = 0;
// If N is not
// divisible by M
if (N % M != 0)
return -1;
// Stores length of
// each partition
int sz = (N / M);
// If K is greater than
// length of partition
if (K > sz)
return -1;
// Sort array in
// descending porder
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;
}
// Traverse the array
for(i = 1; i <= M; i++)
{
// Update maxSum
maxSum += arr[i * K - 1];
}
return maxSum;
}
// Driver code
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));
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program to implement
# the above approach
# Function to find the maximum sum of Kth
# largest element of M equal length partition
def maximumKthLargestsumPart(arr, N, M, K):
# Stores sum of K_th largest element
# of equal length partitions
maxSum = 0
# If N is not
# divisible by M
if (N % M != 0):
return -1
# Stores length of
# each partition
sz = (N / M)
# If K is greater than
# length of partition
if (K > sz):
return -1
# Sort array in
# descending porder
arr = sorted(arr)
for i in range(0, N // 2):
t = arr[i]
arr[i] = arr[N - i - 1]
arr[N - i - 1] = t
# Traverse the array
for i in range(1, M + 1):
# Update maxSum
maxSum += arr[i * K - 1]
return maxSum
# Driver code
if __name__ == '__main__':
arr = [ 1, 2, 3, 4, 5, 6 ]
M = 2
K = 1
N = len(arr)
print(maximumKthLargestsumPart(arr, N, M, K))
# This code is contributed by Amit Katiyar
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to find the maximum sum of Kth
// largest element of M equal length partition
static int maximumKthLargestsumPart(int[] arr, int N,
int M, int K)
{
// Stores sum of K_th largest element
// of equal length partitions
int maxSum = 0;
// If N is not
// divisible by M
if (N % M != 0)
return -1;
// Stores length of
// each partition
int sz = (N / M);
// If K is greater than
// length of partition
if (K > sz)
return -1;
// Sort array in
// descending porder
Array.Sort(arr);
Array.Reverse(arr);
// Traverse the array
for(int i = 1; i <= M; i++)
{
// Update maxSum
maxSum += arr[i * K - 1];
}
return maxSum;
}
// Driver code
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));
}
}
// This code is contributed by divyeshrabadiya07
JavaScript
<script>
// javascript program to implement
// the above approach
// Function to find the maximum sum of Kth
// largest element of M equal length partition
function maximumKthLargestsumPart(arr, N,
M, K)
{
// Stores sum of K_th largest element
// of equal length partitions
let maxSum = 0;
// If N is not
// divisible by M
if (N % M != 0)
return -1;
// Stores length of
// each partition
let sz = (N / M);
// If K is greater than
// length of partition
if (K > sz)
return -1;
// Sort array in
// descending porder
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;
}
// Traverse the array
for(i = 1; i <= M; i++)
{
// Update maxSum
maxSum += arr[i * K - 1];
}
return maxSum;
}
// Driver code
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));
// This code is contributed by splevel62.
</script>
Time complexity: O(N * log(N))
Auxiliary space: O(1)
Similar Reads
Split array into K subsets to maximize sum of their second largest elements Given an array arr[] consisting of N integers and an integer K, the task is to split the array into K subsets (N % K = 0) such that the sum of second largest elements of all subsets is maximized. Examples: Input: arr[] = {1, 3, 1, 5, 1, 3}, K = 2Output: 4Explanation: Splitting the array into the sub
5 min read
Split array into maximum possible subsets having product of their length with the maximum element at least K Given an array arr[] consisting of N integers and a positive integer K, the task is to maximize the number of subsets having a product of its size and its maximum element at least K by dividing array element into disjoint subsets. Examples: Input: N = 5, K = 4, arr[] = {1, 5, 4, 2, 3}Output: 3Explan
5 min read
Largest Subset with sum less than each Array element Given an array arr[] containing N elements, the task is to find the size of the largest subset for each array element arr[i] such that the sum of the subset is less than that element. Examples: Input: arr[] = { 5, 2, 1, 1, 1, 6, 8}Output: 3 1 0 0 0 4 4 Explanation: For i = 0 -> subset = {1, 1, 1}
12 min read
Largest Subset with sum at most K when one Array element can be halved Given an array arr[] of size N and an integer K, the task is to find the size of the largest subset possible having a sum at most K when only one element can be halved (the halved value will be rounded to the closest greater integer). Examples: Input: arr[] = {4, 4, 5}, K = 15Output: 3Explanation: 4
5 min read
Minimize splits in given Array to find subsets of at most 2 elements with sum at most K Given an array arr[] of N integers and an integer K, the task is to calculate the minimum number of subsets of almost 2 elements the array can be divided such that the sum of elements in each subset is almost K. Examples: Input: arr[] = {1, 2, 3}, K = 3Output: 2Explanation: The given array can be di
6 min read