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

• Difficulty Level : Medium
• Last Updated : 19 Mar, 2022

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 minimized sum.
Examples:

Input: arr[] = {1, 1, 2, 2}
Output:
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 minimize 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`

## Javascript

 ``

Output:

`14`

Time Complexity: O(n * log n)

Auxiliary Space: O(1)

My Personal Notes arrow_drop_up