Open In App

Split array into K subsets to maximize sum of their second largest elements

Last Updated : 10 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 = 2
Output: 4
Explanation: Splitting the array into the subsets {1, 1, 3} and {1, 3, 5} maximizes the sum of second maximum elements in the two arrays.

Input: arr[] = {1, 2, 5, 8, 6, 4, 3, 4, 9}, K = 3
Output: 17

Approach: The idea is to sort the array and keep adding every second element encountered while traversing the array in reverse, starting from the second largest element in the array, exactly K times. Follow the steps below to solve the problem:

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 split array into
// K subsets having maximum
// sum of their second maximum elements
void splitArray(int arr[], int n, int K)
{
    // Sort the array
    sort(arr, arr + n);
 
    int i = n - 1;
 
    // Stores the maximum possible
    // sum of second maximums
    int result = 0;
 
    while (K--) {
 
        // Add second maximum
        // of current subset
        result += arr[i - 1];
 
        // Proceed to the second
        // maximum of next subset
        i -= 2;
    }
 
    // Print the maximum
    // sum obtained
    cout << result;
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 1, 3, 1, 5, 1, 3 };
 
    // Size of array
    int N = sizeof(arr)
            / sizeof(arr[0]);
 
    int K = 2;
 
    // Function Call
    splitArray(arr, N, K);
 
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
import java.util.Arrays;
   
class GFG
{
   
// Function to split array into
// K subsets having maximum
// sum of their second maximum elements
static void splitArray(int arr[], int n, int K)
{
    // Sort the array
    Arrays.sort(arr);
  
    int i = n - 1;
  
    // Stores the maximum possible
    // sum of second maximums
    int result = 0;
  
    while (K-- != 0)
    {
  
        // Add second maximum
        // of current subset
        result += arr[i - 1];
  
        // Proceed to the second
        // maximum of next subset
        i -= 2;
    }
  
    // Print the maximum
    // sum obtained
    System.out.print(result);
}
   
// Drive Code
public static void main(String[] args)
{
    // Given array arr[]
    int[] arr = { 1, 3, 1, 5, 1, 3 };
  
    // Size of array
    int N = arr.length;
  
    int K = 2;
  
    // Function Call
    splitArray(arr, N, K);
}
}
 
// This code is contributed by sanjoy_62.


Python3




# Python3 program to implement
# the above approach
 
# Function to split array into K
# subsets having maximum sum of
# their second maximum elements
def splitArray(arr, n, K):
     
    # Sort the array
    arr.sort()
 
    i = n - 1
 
    # Stores the maximum possible
    # sum of second maximums
    result = 0
 
    while (K > 0):
 
        # Add second maximum
        # of current subset
        result += arr[i - 1]
 
        # Proceed to the second
        # maximum of next subset
        i -= 2
        K -= 1
 
    # Print the maximum
    # sum obtained
    print(result)
 
# Driver Code
if __name__ == "__main__":
 
    # Given array arr[]
    arr = [ 1, 3, 1, 5, 1, 3 ]
 
    # Size of array
    N = len(arr)
 
    K = 2
 
    # Function Call
    splitArray(arr, N, K)
 
# This code is contributed by chitranayal


C#




// C# program for the above approach
using System;
class GFG
{
   
// Function to split array into
// K subsets having maximum
// sum of their second maximum elements
static void splitArray(int []arr, int n, int K)
{
    // Sort the array
    Array.Sort(arr);
  
    int i = n - 1;
  
    // Stores the maximum possible
    // sum of second maximums
    int result = 0;
  
    while (K-- != 0)
    {
  
        // Add second maximum
        // of current subset
        result += arr[i - 1];
  
        // Proceed to the second
        // maximum of next subset
        i -= 2;
    }
  
    // Print the maximum
    // sum obtained
    Console.Write(result);
}
   
// Drive Code
public static void Main(String[] args)
{
    // Given array []arr
    int[] arr = { 1, 3, 1, 5, 1, 3 };
  
    // Size of array
    int N = arr.Length;
  
    int K = 2;
  
    // Function Call
    splitArray(arr, N, K);
}
}
 
// This code is contributed by shikhasingrajput


Javascript




<script>
 
// JavaScript program to implement
// the above approach
 
// Function to split array into
// K subsets having maximum
// sum of their second maximum elements
function splitArray(arr, n, K)
{
     
    // Sort the array
    arr.sort();
   
    let i = n - 1;
   
    // Stores the maximum possible
    // sum of second maximums
    let result = 0;
   
    while (K-- != 0)
    {
   
        // Add second maximum
        // of current subset
        result += arr[i - 1];
   
        // Proceed to the second
        // maximum of next subset
        i -= 2;
    }
   
    // Print the maximum
    // sum obtained
    document.write(result);
}
 
// Driver code
 
// Given array arr[]
let arr = [ 1, 3, 1, 5, 1, 3 ];
 
// Size of array
let N = arr.length;
 
let K = 2;
 
// Function Call
splitArray(arr, N, K);
 
// This code is contributed by avijitmondal1998
 
</script>


Output: 

4

 

Time complexity: O(N logN)
Auxiliary Space: O(N)



Similar Reads

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
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 = 2, A[ ] = {1, 13, 7, 17, 6, 5} Output: 37 Expla
6 min read
Split array into minimum number of subsets such that elements of all pairs are present in different subsets at least once
Given an array arr[] consisting of N distinct integers, the task is to find the minimum number of times the array needs to be split into two subsets such that elements of each pair are present into two different subsets at least once. Examples: Input: arr[] = { 3, 4, 2, 1, 5 } Output: 3 Explanation: Possible pairs are { (1, 2), (1, 3), (1, 4), (1,
6 min read
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
Count ways to split array into pair of subsets with difference between their sum equal to K
Given an array arr[] consisting of N integers and an integer K, the task is to find the number of ways to split the array into a pair of subsets such that the difference between their sum is K. Examples: Input: arr[] = {1, 1, 2, 3}, K = 1Output: 3Explanation:Following splits into a pair of subsets satisfies the given condition: {1, 1, 2}, {3}, diff
15 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}, S[] = {1, 3}Output: 48Explanation: Consider spl
7 min read
Count ways to split array into two subsets having difference between their sum equal to K
Given an array A[] of size N and an integer diff, the task is to count the number of ways to split the array into two subsets (non-empty subset is possible) such that the difference between their sums is equal to diff. Examples: Input: A[] = {1, 1, 2, 3}, diff = 1 Output: 3 Explanation: All possible combinations are as follows: {1, 1, 2} and {3}{1,
14 min read
Split array into equal length subsets with maximum sum of Kth largest element of each subset
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 =
7 min read
Split N powers of 2 into two subsets such that their difference of sum is minimum
Given an even number N, the task is to split all N powers of 2 into two sets such that the difference of their sum is minimum.Examples: Input: n = 4 Output: 6 Explanation: Here n = 4 which means we have 21, 22, 23, 24. The most optimal way to divide it into two groups with equal element is 24 + 21 in one group and 22 + 23 in another group giving a
6 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
Article Tags :