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 = 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:
- Sort the array in ascending order.
- Traverse the array arr[] in reverse.
- Initialize the i = N – 1.
- Iterate K times and add arr[i – 1] to the sum and reduce i by 2.
- Finally, print the sum obtained.
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 elementsvoid 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 Codeint 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 approachimport java.io.*;import java.util.Arrays; class GFG{ // Function to split array into// K subsets having maximum// sum of their second maximum elementsstatic 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 Codepublic 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 elementsdef 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 Codeif __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 approachusing System;class GFG{ // Function to split array into// K subsets having maximum// sum of their second maximum elementsstatic 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 Codepublic 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 elementsfunction 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 arraylet N = arr.length;let K = 2;// Function CallsplitArray(arr, N, K);// This code is contributed by avijitmondal1998</script> |
4
Time complexity: O(N logN)
Auxiliary Space: O(N)


Please Login to comment...