Open In App

Split array into equal length subsets with maximum sum of Kth largest element of each subset

Last Updated : 21 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:
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>


Output: 

11

 

Time complexity: O(N * log(N))
Auxiliary space: O(1)



Similar Reads

Split array into K-length subsets to minimize sum of second smallest element of each subset
Given an array arr[] of size N and an integer K (N % K = 0), the task is to split array into subarrays of size K such that the sum of 2nd smallest elements of each subarray is the minimum possible. Examples: Input: arr[] = {11, 20, 5, 7, 8, 14, 2, 17, 16, 10}, K = 5Output: 13Explanation: Splitting array into subsets {11, 5, 14, 2, 10} and {20, 7, 8
5 min read
Split array into two equal length subsets such that all repetitions of a number lies in a single subset
Given an array arr[] consisting of N integers, the task is to check if it is possible to split the integers into two equal length subsets such that all repetitions of any array element belong to the same subset. If found to be true, print "Yes". Otherwise, print "No". Examples: Input: arr[] = {2, 1, 2, 3}Output: YesExplanation:One possible way of d
19 min read
Minimize cost to split an array into K subsets such that the cost of each element is its product with its position in the subset
Given an array arr[] of size N and a positive integer K, the task is to find the minimum possible cost to split the array into K subsets, where the cost of ith element ( 1-based indexing ) of each subset is equal to the product of that element and i. Examples: Input: arr[] = { 2, 3, 4, 1 }, K = 3 Output: 11 Explanation: Split the array arr[] into K
7 min read
Minimum length of a rod that can be split into N equal parts that can further be split into given number of equal parts
Given an array arr[] consisting of N positive integers, the task is to find the minimum possible length of a rod that can be cut into N equal parts such that every ith part can be cut into arr[i] equal parts. Examples: Input: arr[] = {1, 2}Output: 4Explanation:Consider the length of the rod as 4. Then it can be divided in 2 equal parts, each having
7 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 array into 3 non-overlapping subsets is {arr[4],
7 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: 3Explanation:The array can be split into 3 possible subse
6 min read
Find maximum subset sum formed by partitioning any subset of array into 2 partitions with equal sum
Given an array A containing N elements. Partition any subset of this array into two disjoint subsets such that both the subsets have an identical sum. Obtain the maximum sum that can be obtained after partitioning. Note: It is not necessary to partition the entire array, that is any element might not contribute to any of the partition. Examples: In
24 min read
Split an Array A[] into Subsets having equal Sum and sizes equal to elements of Array B[]
Given an array A[] consisting of N integers, the task is to split the array A[] into subsets having equal sum and of length equal to elements in array B[]. Examples: Input: A[] = {17, 13, 21, 20, 50, 29}, B[] = {2, 3, 1} Output: 21 29 17 13 20 50 Input: A[] = { 1, 2, 3, 4, 5, 6}, B[] = { 2, 2, 2} Output: 1 6 2 5 3 4 Approach: Follow the steps below
8 min read
Split array into minimum number of subsets with every element of a subset divisible by its minimum
Given an array arr[] of size N, the task is to split the array into the minimum number of subsets such that every element belongs to exactly one subset and is divisible by the minimum element present in each subset. Examples: Input: arr[] = {10, 2, 3, 5, 4, 2}Output: 3Explanation:The three possible groups are: {5, 10}, where all the element is divi
7 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, 9, 5}, K = 10Output: 2Explanation:All such disjoi
8 min read
Article Tags :
Practice Tags :