Skip to content
Related Articles

Related Articles

Improve Article

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

  • Difficulty Level : Medium
  • Last Updated : 21 Apr, 2021

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 leto
// 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)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :