Given an integer **K** and an array **A[ ]** whose length is multiple of **K**, the task is to split the elements of the given array into **K** subsets, each having equal number of elements, such that the sum of the maximum and minimum elements of each subset is the maximum summation possible.**Examples:**

Input:K = 2, A[ ] = {1, 13, 7, 17, 6, 5}Output:37Explanation:

1st group: {1, 5, 17} maximum = 17, minimum = 1

2nd group: {6, 7, 13} maximum = 13, minimum = 6

Hence, maximum possible sum = 17 + 1 + 13 + 6 = 37Input:K = 2, A[ ] = {10, 10, 10, 10, 11, 11}Output:42Explanation:

1st group: {11, 10, 10} maximum = 11, minimum = 10

2nd group: {11, 10, 10} maximum = 11, minimum = 10

Hence, maximum sum possible = 11 + 10 + 11 + 10 = 42

**Naive Approach:**

The simplest approach to solve this problem is to generate all possible groups of **K** subsets of size **N/K** and for each group, find maximum and minimum in every subset and calculate their sum. Once the sum of all groups are calculated, print the maximum sum obtained. **Time Complexity:** O(2^{N})**Auxiliary Space:** O(N)**Efficient Approach:**

The idea is to optimize the above approach using Greedy Technique. Since the maximum sum of the maximum and minimum element from each subset is needed, try to maximize the maximum element and minimum element. For the maximum element of each subset, take first **K** largest elements from the given array and insert each one to different subsets. For the minimum element of each subset, from the sorted array, starting from index **0**, pick every next element at **( N / K ) – 1** interval since the size of each subset is **N / K** and each one already contains a maximum element.

Follow the steps below:

- Calculate the number of elements in each group i.e.
**(N/K)**. - Sort all the elements of
**A[ ]**in non-descending order. - For the sum of
**maximum**elements, add all**K**largest elements from sorted array. - For sum of
**minimum**elements, starting from index**0**, select**K**elements each with**(N / K) – 1**intervals and add them. - Finally, calculate the sum of maximum and the sum of minimum elements. Print the sum of their respective sums as the final answer.

Below is the implementation of the above approach:

## C++

`// C++ Program to implement` `// the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function that prints` `// the maximum sum possible` `void` `maximumSum(` `int` `arr[],` ` ` `int` `n, ` `int` `k)` `{` ` ` `// Find elements in each group` ` ` `int` `elt = n / k;` ` ` `int` `sum = 0;` ` ` `// Sort all elements in` ` ` `// non-descending order` ` ` `sort(arr, arr + n);` ` ` `int` `count = 0;` ` ` `int` `i = n - 1;` ` ` `// Add K largest elements` ` ` `while` `(count < k) {` ` ` `sum += arr[i];` ` ` `i--;` ` ` `count++;` ` ` `}` ` ` `count = 0;` ` ` `i = 0;` ` ` `// For sum of minimum` ` ` `// elements from each subset` ` ` `while` `(count < k) {` ` ` `sum += arr[i];` ` ` `i += elt - 1;` ` ` `count++;` ` ` `}` ` ` `// Printing the maximum sum` ` ` `cout << sum << ` `"\n"` `;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `Arr[] = { 1, 13, 7, 17, 6, 5 };` ` ` `int` `K = 2;` ` ` `int` `size = ` `sizeof` `(Arr) / ` `sizeof` `(Arr[0]);` ` ` `maximumSum(Arr, size, K);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement ` `// the above approach ` `import` `java.util.Arrays;` `class` `GFG{ ` ` ` `// Function that prints` `// the maximum sum possible` `static` `void` `maximumSum(` `int` `arr[],` ` ` `int` `n, ` `int` `k)` `{` ` ` ` ` `// Find elements in each group` ` ` `int` `elt = n / k;` ` ` `int` `sum = ` `0` `;` ` ` `// Sort all elements in` ` ` `// non-descending order` ` ` `Arrays.sort(arr);` ` ` `int` `count = ` `0` `;` ` ` `int` `i = n - ` `1` `;` ` ` `// Add K largest elements` ` ` `while` `(count < k)` ` ` `{` ` ` `sum += arr[i];` ` ` `i--;` ` ` `count++;` ` ` `}` ` ` `count = ` `0` `;` ` ` `i = ` `0` `;` ` ` `// For sum of minimum` ` ` `// elements from each subset` ` ` `while` `(count < k)` ` ` `{` ` ` `sum += arr[i];` ` ` `i += elt - ` `1` `;` ` ` `count++;` ` ` `}` ` ` `// Printing the maximum sum` ` ` `System.out.println(sum);` `}` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `Arr[] = { ` `1` `, ` `13` `, ` `7` `, ` `17` `, ` `6` `, ` `5` `};` ` ` `int` `K = ` `2` `;` ` ` `int` `size = Arr.length;` ` ` `maximumSum(Arr, size, K); ` `} ` `} ` `// This code is contributed by Shubham Prakash ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement` `# the above approach` `# Function that prints` `# the maximum sum possible` `def` `maximumSum(arr, n, k):` ` ` ` ` `# Find elements in each group` ` ` `elt ` `=` `n ` `/` `/` `k;` ` ` `sum` `=` `0` `;` ` ` `# Sort all elements in` ` ` `# non-descending order` ` ` `arr.sort();` ` ` `count ` `=` `0` `;` ` ` `i ` `=` `n ` `-` `1` `;` ` ` `# Add K largest elements` ` ` `while` `(count < k):` ` ` `sum` `+` `=` `arr[i];` ` ` `i ` `-` `=` `1` `;` ` ` `count ` `+` `=` `1` `;` ` ` `count ` `=` `0` `;` ` ` `i ` `=` `0` `;` ` ` `# For sum of minimum` ` ` `# elements from each subset` ` ` `while` `(count < k):` ` ` `sum` `+` `=` `arr[i];` ` ` `i ` `+` `=` `elt ` `-` `1` `;` ` ` `count ` `+` `=` `1` `;` ` ` `# Printing the maximum sum` ` ` `print` `(` `sum` `);` `# Driver code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `Arr ` `=` `[` `1` `, ` `13` `, ` `7` `, ` `17` `, ` `6` `, ` `5` `];` ` ` `K ` `=` `2` `;` ` ` `size ` `=` `len` `(Arr);` ` ` `maximumSum(Arr, size, K);` `# This code is contributed by sapnasingh4991` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement ` `// the above approach ` `using` `System;` `class` `GFG{ ` ` ` `// Function that prints` `// the maximum sum possible` `static` `void` `maximumSum(` `int` `[]arr,` ` ` `int` `n, ` `int` `k)` `{` ` ` ` ` `// Find elements in each group` ` ` `int` `elt = n / k;` ` ` `int` `sum = 0;` ` ` `// Sort all elements in` ` ` `// non-descending order` ` ` `Array.Sort(arr);` ` ` `int` `count = 0;` ` ` `int` `i = n - 1;` ` ` `// Add K largest elements` ` ` `while` `(count < k)` ` ` `{` ` ` `sum += arr[i];` ` ` `i--;` ` ` `count++;` ` ` `}` ` ` `count = 0;` ` ` `i = 0;` ` ` `// For sum of minimum` ` ` `// elements from each subset` ` ` `while` `(count < k)` ` ` `{` ` ` `sum += arr[i];` ` ` `i += elt - 1;` ` ` `count++;` ` ` `}` ` ` `// Printing the maximum sum` ` ` `Console.WriteLine(sum);` `}` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `[]Arr = { 1, 13, 7, 17, 6, 5 };` ` ` `int` `K = 2;` ` ` `int` `size = Arr.Length;` ` ` `maximumSum(Arr, size, K); ` `} ` `} ` `// This code is contributed by amal kumar choubey` |

*chevron_right*

*filter_none*

**Output:**

37

**Time complexity:** O(N*logN)**Auxiliary Space:** O(1)

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.

## Recommended Posts:

- Split N powers of 2 into two subsets such that their difference of sum is minimum
- Split an Array A[] into Subsets having equal Sum and sizes equal to elements of Array B[]
- Check if it is possible to split given Array into K odd-sum subsets
- Split a binary string into K subsets minimizing sum of products of occurrences of 0 and 1
- Split array into two subarrays such that difference of their sum is minimum
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Rearrange an Array such that Sum of same-indexed subsets differ from their Sum in the original Array
- Split numbers from 1 to N into two equal sum subsets
- Split a given array into K subarrays minimizing the difference between their maximum and minimum
- Check if an array can be split into subsets of K consecutive elements
- Split Array into min number of subsets with difference between each pair greater than 1
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Maximum sum of minimums of pairs in an array
- Split array into two subarrays such that difference of their maximum is minimum
- Sum of subsets of all the subsets of an array | O(3^N)
- Sum of subsets of all the subsets of an array | O(2^N)
- Sum of subsets of all the subsets of an array | O(N)
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Split N natural numbers into two sets having GCD of their sums greater than 1
- Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.