Given an array **arr[]** of **N** integers where **N % 4 = 0**, the task is to divide the integers into groups of four such that when the combined sum of the maximum two elements from all the groups is taken, it is minimum possible. Print the minimised sum.

**Examples:**

Input:arr[] = {1, 1, 2, 2}

Output:4

The only group will be {1, 1, 2, 2}.

2 + 2 = 4

Input:arr[] = {1, 1, 10, 2, 2, 2, 1, 8}

Output:21

{1, 1, 2, 1} and {10, 2, 2, 8} are the groups that will

give the minimum sum as 1 + 2 + 10 + 8 = 21.

**Approach:** In order to minimise the sum, the maximum four elements from the array must be in the same group because the maximum two elements will definitely be included in the sum no matter what group they are a part of but the next two maximum elements can be prevented if they are part of this group. Making groups in the same manner will give the minimum sum possible. So, sort the array in descending order and starting from the first element, make groups of four consecutive elements.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the minimum required sum ` `int` `minSum(` `int` `arr[], ` `int` `n) ` `{ ` ` ` ` ` `// To store the required sum ` ` ` `int` `sum = 0; ` ` ` ` ` `// Sort the array in descending order ` ` ` `sort(arr, arr + n, greater<` `int` `>()); ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// The indices which give 0 or 1 as ` ` ` `// the remainder when divided by 4 ` ` ` `// will be the maximum two ` ` ` `// elements of the group ` ` ` `if` `(i % 4 < 2) ` ` ` `sum = sum + arr[i]; ` ` ` `} ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 1, 10, 2, 2, 2, 1 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout << minSum(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the minimum required sum ` `static` `int` `minSum(Integer arr[], ` `int` `n) ` `{ ` ` ` ` ` `// To store the required sum ` ` ` `int` `sum = ` `0` `; ` ` ` ` ` `// Sort the array in descending order ` ` ` `Arrays.sort(arr, Collections.reverseOrder()); ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` ` ` `// The indices which give 0 or 1 as ` ` ` `// the remainder when divided by 4 ` ` ` `// will be the maximum two ` ` ` `// elements of the group ` ` ` `if` `(i % ` `4` `< ` `2` `) ` ` ` `sum = sum + arr[i]; ` ` ` `} ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `Integer []arr = { ` `1` `, ` `1` `, ` `10` `, ` `2` `, ` `2` `, ` `2` `, ` `1` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `System.out.println(minSum(arr, n)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the minimum required sum ` `def` `minSum(arr, n) : ` ` ` ` ` `# To store the required sum ` ` ` `sum` `=` `0` `; ` ` ` ` ` `# Sort the array in descending order ` ` ` `arr.sort(reverse ` `=` `True` `) ` ` ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` ` ` `# The indices which give 0 or 1 as ` ` ` `# the remainder when divided by 4 ` ` ` `# will be the maximum two ` ` ` `# elements of the group ` ` ` `if` `(i ` `%` `4` `< ` `2` `) : ` ` ` `sum` `+` `=` `arr[i]; ` ` ` ` ` `return` `sum` `; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `arr ` `=` `[ ` `1` `, ` `1` `, ` `10` `, ` `2` `, ` `2` `, ` `2` `, ` `1` `]; ` ` ` `n ` `=` `len` `(arr); ` ` ` `print` `(minSum(arr, n)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the minimum required sum ` `static` `int` `minSum(` `int` `[]arr, ` `int` `n) ` `{ ` ` ` ` ` `// To store the required sum ` ` ` `int` `sum = 0; ` ` ` ` ` `// Sort the array in descending order ` ` ` `Array.Sort(arr); ` ` ` `Array.Reverse(arr); ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` ` ` `// The indices which give 0 or 1 as ` ` ` `// the remainder when divided by 4 ` ` ` `// will be the maximum two ` ` ` `// elements of the group ` ` ` `if` `(i % 4 < 2) ` ` ` `sum = sum + arr[i]; ` ` ` `} ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `[]arr = { 1, 1, 10, 2, 2, 2, 1 }; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `Console.WriteLine(minSum(arr, n)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

14

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:

- Divide Matrix into K groups of adjacent cells having minimum difference between maximum and minimum sized groups
- Number of ways of distributing N identical objects in R distinct groups with no groups empty
- Minimum sum subsequence such that at least one of every four consecutive elements is picked
- Find four elements that sum to a given value | Set 1 (n^3 solution)
- Find four elements that sum to a given value | Set 3 (Hashmap)
- Find four elements that sum to a given value | Two-Pointer approach
- Find four elements that sum to a given value | Set 2
- Minimum sum obtained by choosing N number from given N pairs
- Array obtained by repeatedly reversing array after every insertion from given array
- Find four elements a, b, c and d in an array such that a+b = c+d
- Find four missing numbers in an array containing elements from 1 to N
- Sum of two numbers if the original ratio and new ratio obtained by adding a given number to each number is given
- Check if sum Y can be obtained from the Array by the given operations
- Cost required to empty a given array by repeated removal of maximum obtained by given operations
- Maximum sum of even indexed elements obtained by right shift on an even sized subarray
- Divide 1 to n into two groups with minimum sum difference
- Find maximum points which can be obtained by deleting elements from array
- Find four factors of N with maximum product and sum equal to N | Set-2
- Find four factors of N with maximum product and sum equal to N
- Find four factors of N with maximum product and sum equal to N | Set 3

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.