Skip to content
Related Articles

Related Articles

Split array into K subsets to maximize sum of their second largest elements
  • Difficulty Level : Basic
  • Last Updated : 08 Dec, 2020

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :