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.
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; } |
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 |
14
Recommended Posts:
- Find maximum points which can be obtained by deleting elements from array
- Flip minimum signs of array elements to get minimum sum of positive elements possible
- Minimum sum of the elements of an array after subtracting smaller elements from larger
- Find minimum value to assign all array elements so that array product becomes greater
- Divide 1 to n into two groups with minimum sum difference
- Minimum difference between groups of size two
- Maximum array sum that can be obtained after exactly k changes
- Remove minimum elements from array so that max <= 2 * min
- Remove minimum elements from the array such that 2*min becomes more than max
- Minimum value among AND of elements of every subset of an array
- Find the first, second and third minimum elements in an array
- Sum of all minimum occurring elements in an Array
- Minimum array element changes to make its elements 1 to N
- Minimum sum of array elements based on given Criteria
- Minimum possible sum of array elements after performing the given operation
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.



