Related Articles
Maximize sum of second minimums of each K length partitions of the array
• Last Updated : 01 Feb, 2021

Given an array A[] of size N and a positive integer K ( which will always be a factor of N), the task is to find the maximum possible sum of the second smallest elements of each partition of the array by partitioning the array into (N / K) partitions of equal size.

Examples:

Input: A[] = {2, 3, 1, 4, 7, 5, 6, 1}, K = 4
Output: 7
Explanation: Split the array as {1, 2, 3, 4} and {1, 5, 6, 7}. Therefore, sum = 2 + 5 = 7, which is the maximum possible sum.

Input: A[] = {12, 43, 15, 32, 45, 23}, K = 3
Output : 66
Explanation: Split the array as {12, 23, 32} and {15, 43, 45}. Therefore, sum = 23 + 43 = 66, which is the maximum possible sum.

Approach: The idea is to sort the given array in ascending order and in order to maximize the required sum, divide the first N / K elements of A[] to each of the arrays as their first term, then choose every (K – 1)th element of A[] starting from N/K

Follow the steps below to solve the problem:

• Sort the array A[] in increasing order.
• Initialize sum with 0 to store the required sum.
• Now, initialize a variable i with N / K.
• While i is less than N, perform the following steps:
• Increment sum by A[i].
• Increment i by K – 1.
• After traversing, print sum as the required answer.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to find the maximum sum of``// second smallest of each partition``// of size K``void` `findSum(``int` `A[], ``int` `N, ``int` `K)``{` `    ``// Sort the array A[]``    ``// in ascending order``    ``sort(A, A + N);` `    ``// Store the maximum sum of second``    ``// smallest of each partition``    ``// of size K``    ``int` `sum = 0;` `    ``// Select every (K-1)th element as``    ``// second smallest element``    ``for` `(``int` `i = N / K; i < N; i += K - 1) {` `        ``// Update sum``        ``sum += A[i];``    ``}` `    ``// Print the maximum sum``    ``cout << sum;``}` `// Driver Code``int` `main()``{` `    ``// Given size of partitions``    ``int` `K = 4;` `    ``// Given array A[]``    ``int` `A[] = { 2, 3, 1, 4, 7, 5, 6, 1 };` `    ``// Size of the given array``    ``int` `N = ``sizeof``(A) / ``sizeof``(A);` `    ``// Function Call``    ``findSum(A, N, K);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;` `class` `GFG{` `// Function to find the maximum sum of``// second smallest of each partition``// of size K``static` `void` `findSum(``int` `A[], ``int` `N, ``int` `K)``{``    ` `    ``// Sort the array A[]``    ``// in ascending order``    ``Arrays.sort(A);``    ` `    ``// Store the maximum sum of second``    ``// smallest of each partition``    ``// of size K``    ``int` `sum = ``0``;` `    ``// Select every (K-1)th element as``    ``// second smallest element``    ``for``(``int` `i = N / K; i < N; i += K - ``1``)``    ``{``        ` `        ``// Update sum``        ``sum += A[i];``    ``}` `    ``// Print the maximum sum``    ``System.out.print(sum);``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ` `    ``// Given size of partitions``    ``int` `K = ``4``;` `    ``// Given array A[]``    ``int` `A[] = { ``2``, ``3``, ``1``, ``4``, ``7``, ``5``, ``6``, ``1` `};` `    ``// Size of the given array``    ``int` `N = A.length;` `    ``// Function Call``    ``findSum(A, N, K);``}``}` `// This code is contributed by shikhasingrajput`

## Python3

 `# Python3 program for the above approach` `# Function to find the maximum sum of``# second smallest of each partition``# of size K``def` `findSum(A, N, K):``  ` `    ``# Sort the array A``    ``# in ascending order``    ``A.sort();` `    ``# Store the maximum sum of second``    ``# smallest of each partition``    ``# of size K``    ``sum` `=` `0``;` `    ``# Select every (K-1)th element as``    ``# second smallest element``    ``for` `i ``in` `range``(N ``/``/` `K, N, K ``-` `1``):``      ` `        ``# Update sum``        ``sum` `+``=` `A[i];` `    ``# Prthe maximum sum``    ``print``(``sum``);` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``  ` `    ``# Given size of partitions``    ``K ``=` `4``;` `    ``# Given array A``    ``A ``=` `[``2``, ``3``, ``1``, ``4``, ``7``, ``5``, ``6``, ``1``];` `    ``# Size of the given array``    ``N ``=` `len``(A);` `    ``# Function Call``    ``findSum(A, N, K);` `    ``# This code contributed by shikhasingrajput`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{` `// Function to find the maximum sum of``// second smallest of each partition``// of size K``static` `void` `findSum(``int` `[]A, ``int` `N, ``int` `K)``{``    ` `    ``// Sort the array []A``    ``// in ascending order``    ``Array.Sort(A);``    ` `    ``// Store the maximum sum of second``    ``// smallest of each partition``    ``// of size K``    ``int` `sum = 0;` `    ``// Select every (K-1)th element as``    ``// second smallest element``    ``for``(``int` `i = N / K; i < N; i += K - 1)``    ``{``        ` `        ``// Update sum``        ``sum += A[i];``    ``}``    ` `    ``// Print the maximum sum``    ``Console.Write(sum);``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ` `    ``// Given size of partitions``    ``int` `K = 4;` `    ``// Given array []A``    ``int` `[]A = { 2, 3, 1, 4, 7, 5, 6, 1 };` `    ``// Size of the given array``    ``int` `N = A.Length;` `    ``// Function Call``    ``findSum(A, N, K);``}``}` `// This code is contributed by shikhasingrajput`
Output:
`7`

Time Complexity: O(N * log(N))
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up