# Minimum sum obtained from groups of four elements from the given array

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.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 ` `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); ` ` `  `    ``cout << minSum(arr, n); ` ` `  `    ``return` `0; ` `} `

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

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

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

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : AnkitRai01, 29AjayKumar

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.