Maximum sum of minimums of pairs in an array | C ++ implementation of the approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSum ( int a [ ] , int n ) { sort ( a , a + n ) ; int sum = 0 ; for ( int i = 0 ; i < n - 1 ; i += 2 ) { sum += a [ i ] ; } return sum ; } int main ( ) { int arr [ ] = { 1 , 3 , 2 , 1 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxSum ( arr , n ) ; return 0 ; }