Minimum sum obtained from groups of four elements from the given array | Java 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
import java . util . * ; class GFG { static int minSum ( Integer arr [ ] , int n ) { int sum = 0 ; Arrays . sort ( arr , Collections . reverseOrder ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 4 < 2 ) sum = sum + arr [ i ] ; } return sum ; } 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 ) ) ; } }