Maximum sum of minimums of pairs in an array
Last Updated :
07 Mar, 2022
Given an array arr[] of N integers where N is even, the task is to group the array elements in the pairs (X1, Y1), (X2, Y2), (X3, Y3), ... such that the sum min(X1, Y1) + min(X2, Y2) + min(X3, Y3) + ... is maximum.
Examples:
Input: arr[] = {1, 5, 3, 2}
Output: 4
(1, 5) and (3, 2) -> 1 + 2 = 3
(1, 3) and (5, 2) -> 1 + 2 = 3
(1, 2) and (5, 3) -> 1 + 3 = 4
Input: arr[] = {1, 3, 2, 1, 4, 5}
Output: 7
Approach: No matter how the pairs are formed, the maximum element from the array will always be ignored as it will be the maximum element in every pair it is put into. Same goes for the second maximum element unless it is paired with the maximum element. So, to maximize the sum an optimal approach will be to sort the array and start making pairs in order starting from the maximum element.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the maximum
// required sum of the pairs
int maxSum(int a[], int n)
{
// Sort the array
sort(a, a + n);
// To store the sum
int sum = 0;
// Start making pairs of every two
// consecutive elements as n is even
for (int i = 0; i < n - 1; i += 2) {
// Minimum element of the current pair
sum += a[i];
}
// Return the maximum possible sum
return sum;
}
// Driver code
int main()
{
int arr[] = { 1, 3, 2, 1, 4, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << maxSum(arr, n);
return 0;
}
Java
// Java implementation of the approach
import java.util.Arrays;
class GFG
{
// Function to return the maximum
// required sum of the pairs
static int maxSum(int a[], int n)
{
// Sort the array
Arrays.sort(a);
// To store the sum
int sum = 0;
// Start making pairs of every two
// consecutive elements as n is even
for (int i = 0; i < n - 1; i += 2)
{
// Minimum element of the current pair
sum += a[i];
}
// Return the maximum possible sum
return sum;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 3, 2, 1, 4, 5 };
int n = arr.length;
System.out.println(maxSum(arr, n));
}
}
// This code is contributed by Code_Mech
Python3
# Python3 implementation of the approach
# Function to return the maximum
# required sum of the pairs
def maxSum(a, n) :
# Sort the array
a.sort();
# To store the sum
sum = 0;
# Start making pairs of every two
# consecutive elements as n is even
for i in range(0, n - 1, 2) :
# Minimum element of the current pair
sum += a[i];
# Return the maximum possible sum
return sum;
# Driver code
if __name__ == "__main__" :
arr = [ 1, 3, 2, 1, 4, 5 ];
n = len(arr);
print(maxSum(arr, n));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the maximum
// required sum of the pairs
static int maxSum(int []a, int n)
{
// Sort the array
Array.Sort(a);
// To store the sum
int sum = 0;
// Start making pairs of every two
// consecutive elements as n is even
for (int i = 0; i < n - 1; i += 2)
{
// Minimum element of the current pair
sum += a[i];
}
// Return the maximum possible sum
return sum;
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 1, 3, 2, 1, 4, 5 };
int n = arr.Length;
Console.WriteLine(maxSum(arr, n));
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// Javascript implementation of the approach
// Function to return the maximum
// required sum of the pairs
function maxSum(a, n) {
// Sort the array
a.sort((a, b) => a - b);
// To store the sum
let sum = 0;
// Start making pairs of every two
// consecutive elements as n is even
for (let i = 0; i < n - 1; i += 2) {
// Minimum element of the current pair
sum += a[i];
}
// Return the maximum possible sum
return sum;
}
// Driver code
let arr = [1, 3, 2, 1, 4, 5];
let n = arr.length;
document.write(maxSum(arr, n));
// This code is contributed by _saurabh_jaiswal
</script>
Time Complexity: O(n logn)
Auxiliary Space: O(1)
Similar Reads
Maximum and minimum sum of Bitwise XOR of pairs from an array Given an array arr[] of size N, the task is to find the maximum and minimum sum of Bitwise XOR of all pairs from an array by splitting the array into N / 2 pairs. Examples: Input: arr[] = {1, 2, 3, 4}Output: 6 10Explanation:Bitwise XOR of the all possible pair splits are as follows:(1, 2), (3, 4) â
11 min read
Minimum and Maximum sum of absolute differences of pairs Given an array of N integers where N is even, find the minimum and maximum sum of absolute difference of N/2 pairs formed by pairing every element with one other element. Examples: Input: a[] = {10, -10, 20, -40} Output: min_sum = 40, max_sum = 80 Explanation: Pairs selected for minimum sum (-10, -4
8 min read
Maximum OR value of a pair in an Array | Set 2 Given an array arr[] of N positive elements, the task is to find the maximum bitwise OR value of a pair from the given array.Examples: Input: arr[] = {3, 6, 8, 16} Output: 24 Explanation: The pair giving maximum OR value is (8, 16) 8|16 = 24Input: arr[] = {8, 7, 3, 12} Output: 15 Explanation: There
5 min read
Maximum OR value of a pair in an Array | Set 2 Given an array arr[] of N positive elements, the task is to find the maximum bitwise OR value of a pair from the given array.Examples: Input: arr[] = {3, 6, 8, 16} Output: 24 Explanation: The pair giving maximum OR value is (8, 16) 8|16 = 24Input: arr[] = {8, 7, 3, 12} Output: 15 Explanation: There
5 min read
Maximum Sum pair of non adjacent elements in array Given an array arr[] of distinct Integers, find the pair with maximum sum such that both elements of the pair are not adjacent in the array Examples: Input: arr[] = {7, 3, 4, 1, 8, 9}Output: 9 7Explanation: Pair with maximum sum is (8, 9). But since both elements are adjacent, it is not a valid pair
8 min read
Maximum of minimums of every window size in a given array Given an integer array arr[] of size n, the task is to find the maximum of the minimums for every window size in the given array, where the window size ranges from 1 to n.Example:Input: arr[] = [10, 20, 30]Output: [30, 20, 10]Explanation: First element in output indicates maximum of minimums of all
14 min read