Related Articles

# Split array into K-length subsets to minimize sum of second smallest element of each subset

• Difficulty Level : Hard
• Last Updated : 07 May, 2021

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 = 5
Output: 13
Explanation: Splitting array into subsets {11, 5, 14, 2, 10} and {20, 7, 8, 17, 16} generates minimum sum of second smallest elements( = 5 + 8 = 13).

Input: arr[] = {13, 19, 20, 5, 17, 11, 10, 8, 23, 14}, K = 5
Output: 19
Explanation: Splitting array into subsets {10, 19, 20, 11, 23} and {17, 5, 8, 13, 14} generates minimum sum of second smallest elements(= 11 + 8 = 19).

Approach: The problem can be solved by sorting technique. Follow the steps below to solve this problem:

Below is the implementation of the above approach:

## C++

 // C++ implementation for above approach #include using namespace std; // Function to find the minimum sum of// 2nd smallest elements of each subsetint findMinimum(int arr[], int N, int K){    // Sort the array    sort(arr, arr + N);     // Stores minimum sum of second    // elements of each subset    int ans = 0;     // Traverse first K 2nd smallest elements    for (int i = 1; i < 2 * (N / K); i += 2) {         // Update their sum        ans += arr[i];    }     // Print the sum    cout << ans;} // Driver Codeint main(){    // Given Array    int arr[] = { 11, 20, 5, 7, 8,                  14, 2, 17, 16, 10 };     // Given size of the array    int N = sizeof(arr)            / sizeof(arr[0]);     // Given subset lengths    int K = 5;     findMinimum(arr, N, K);     return 0;}

## Java

 // Java implementation for the above approach import java.io.*;import java.util.*; class GFG {     // Function to find the minimum sum of    // 2nd smallest elements of each subset    public static void findMinimum(        int arr[], int N, int K)    {        // Sort the array        Arrays.sort(arr);         // Stores minimum sum of second        // elements of each subset        int ans = 0;         // Traverse first K 2nd smallest elements        for (int i = 1; i < 2 * (N / K); i += 2) {             // Update their sum            ans += arr[i];        }         // Print the sum        System.out.println(ans);    }     // Driver Code    public static void main(String[] args)    {        // Given Array        int[] arr = { 11, 20, 5, 7, 8,                      14, 2, 17, 16, 10 };         // Given length of array        int N = arr.length;         // Given subset lengths        int K = 5;         findMinimum(arr, N, K);    }}

## Python3

 # Python3 implementation for above approach # Function to find the minimum sum of# 2nd smallest elements of each subsetdef findMinimum(arr, N, K):       # Sort the array    arr = sorted(arr)     # Stores minimum sum of second    # elements of each subset    ans = 0     # Traverse first K 2nd smallest elements    for i in range(1, 2 * (N//K), 2):         # Update their sum        ans += arr[i]     # Prthe sum    print (ans) # Driver Codeif __name__ == '__main__':       # Given Array    arr = [11, 20, 5, 7, 8, 14, 2, 17, 16, 10]     # Given size of the array    N = len(arr)     # Given subset lengths    K = 5    findMinimum(arr, N, K) # This code is contributed by mohit kumar 29

## C#

 // C# implementation for above approachusing System;  class GFG{  // Function to find the minimum sum of// 2nd smallest elements of each subsetpublic static void findMinimum(int[] arr, int N,                               int K){         // Sort the array    Array.Sort(arr);     // Stores minimum sum of second    // elements of each subset    int ans = 0;     // Traverse first K 2nd smallest elements    for(int i = 1; i < 2 * (N / K); i += 2)    {                 // Update their sum        ans += arr[i];    }     // Print the sum    Console.WriteLine(ans);} // Driver Codepublic static void Main(){         // Given Array    int[] arr = { 11, 20, 5, 7, 8,                  14, 2, 17, 16, 10 };     // Given length of array    int N = arr.Length;     // Given subset lengths    int K = 5;     findMinimum(arr, N, K);}} // This code is contributed by susmitakundugoaldanga

## Javascript



Output:
13

Time Complexity: O(NlogN)
Space Complexity: 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