Maximum sum of minimums of pairs in an array | Function to return the maximum required sum of the pairs ; Sort the array ; To store the sum ; Start making pairs of every two consecutive elements as n is even ; Minimum element of the current pair ; Return the maximum possible sum ; Driver code
function maxSum ( a , n ) { a . sort ( ( a , b ) => a - b ) ; let sum = 0 ; for ( let i = 0 ; i < n - 1 ; i += 2 ) { sum += a [ i ] ; } return sum ; } let arr = [ 1 , 3 , 2 , 1 , 4 , 5 ] ; let n = arr . length ; document . write ( maxSum ( arr , n ) ) ;