Minimum sum obtained from groups of four elements from the given array | 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
function minSum ( arr , n ) { let sum = 0 ; arr . sort ( function ( a , b ) { return b - a } ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( i % 4 < 2 ) sum = sum + arr [ i ] ; } return sum ; } let arr = [ 1 , 1 , 10 , 2 , 2 , 2 , 1 ] ; let n = arr . length ; document . write ( minSum ( arr , n ) ) ;