Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Minimize sum of smallest elements from K subsequences of length L
Next article icon

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

Last Updated : 18 Jan, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

  • Sort the given array in ascending order.
  • Since all subsets are of length Kgroups, choose first K odd-indexed elements starting from 1 and calculate their sum.
  • Print the obtained sum.

Below is the implementation of the above approach:

C++




// C++ implementation for above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum sum of
// 2nd smallest elements of each subset
int 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 Code
int 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 subset
def 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]
 
    # Print the sum
    print (ans)
 
# Driver Code
if __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 approach
using System;
  
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
    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 Code
public 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




<script>
 
// Javascript implementation for
// the above approach
 
    // Function to find the minimum sum of
    // 2nd smallest elements of each subset
    function findMinimum(arr , N , K)
    {
        // Sort the array
        arr.sort((a,b)=>a-b);
 
        // Stores minimum sum of second
        // elements of each subset
        var ans = 0;
 
        // Traverse first K 2nd smallest elements
        for (i = 1; i < 2 * (parseInt(N / K)); i += 2)
        {
 
            // Update their sum
            ans += arr[i];
        }
 
        // Print the sum
        document.write(ans);
    }
 
    // Driver Code
     
        // Given Array
        var arr = [ 11, 20, 5, 7, 8, 14, 2, 17, 16, 10 ];
 
        // Given length of array
        var N = arr.length;
 
        // Given subset lengths
        var K = 5;
 
        findMinimum(arr, N, K);
 
// This code is contributed by todaysgaurav
 
</script>
 
 

 
 

Output: 
13

 

 

Time Complexity: O(NlogN)
Space Complexity: O(N)

 



Next Article
Minimize sum of smallest elements from K subsequences of length L

R

RohitOberoi
Improve
Article Tags :
  • Arrays
  • DSA
  • Greedy
  • Mathematical
  • Sorting
  • array-rearrange
  • subset
Practice Tags :
  • Arrays
  • Greedy
  • Mathematical
  • Sorting
  • subset

Similar Reads

  • 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 = 2Output: 4Explanation: Splitting the array into the sub
    5 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. Example
    7 min read
  • Minimize sum of smallest elements from K subsequences of length L
    Given an array arr[] of size N, the task is to find the minimum possible sum by extracting the smallest element from any K subsequences from arr[] of length L such that each of the subsequences have no shared element. If it is not possible to get the required sum, print -1. Examples: Input: arr[] =
    5 min read
  • Smallest subset of maximum sum possible by splitting array into two subsets
    Given an array arr[] consisting of N integers, the task is to print the smaller of the two subsets obtained by splitting the array into two subsets such that the sum of the smaller subset is maximized. Examples: Input: arr[] = {5, 3, 2, 4, 1, 2}Output: 4 5Explanation:Split the array into two subsets
    11 min read
  • Minimize cost to split an array into K subsets such that the cost of each element is its product with its position in the subset
    Given an array arr[] of size N and a positive integer K, the task is to find the minimum possible cost to split the array into K subsets, where the cost of ith element ( 1-based indexing ) of each subset is equal to the product of that element and i. Examples: Input: arr[] = { 2, 3, 4, 1 }, K = 3 Ou
    7 min read
  • Minimize splits in given Array to find subsets of at most 2 elements with sum at most K
    Given an array arr[] of N integers and an integer K, the task is to calculate the minimum number of subsets of almost 2 elements the array can be divided such that the sum of elements in each subset is almost K. Examples: Input: arr[] = {1, 2, 3}, K = 3Output: 2Explanation: The given array can be di
    6 min read
  • Minimum size of subset of pairs whose sum is at least the remaining array elements
    Given two arrays A[] and B[] both consisting of N positive integers, the task is to find the minimum size of the subsets of pair of elements (A[i], B[i]) such that the sum of all the pairs of subsets is at least the sum of remaining array elements A[] which are not including in the subset i.e., (A[0
    11 min read
  • Sum of length of two smallest subsets possible from a given array with sum at least K
    Given an array arr[] consisting of N integers and an integer K, the task is to find the sum of the length of the two smallest unique subsets having sum of its elements at least K. Examples: Input: arr[] = {2, 4, 5, 6, 7, 8}, K = 16Output: 6Explanation:The subsets {2, 6, 8} and {4, 5, 7} are the two
    15+ min read
  • Minimize cost by splitting given Array into subsets of size K and adding highest K/2 elements of each subset into cost
    Given an array arr[] of N integers and an integer K, the task is to calculate the minimum cost by spilling the array elements into subsets of size K and adding the maximum ⌈K/2⌉ elements into the cost. Note: ⌈K/2⌉ means ceiling value of K/2. Examples: Input: arr[] = {1, 1, 2, 2}, K = 2Output: 3Expla
    5 min read
  • 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,
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences