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