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 = 4Output:7Explanation: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 = 3Output :66Explanation: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**.

Fullow the steps below to sulve 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**.

- Increment
- After traversing, print
**sum**as the required answer.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `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[0]);` ` ` `// Function Call` ` ` `findSum(A, N, K);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

**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 DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.