Maximum sum of pairs with specific difference C++ program


In this problem, we are given an array arr[] of n integers and a number d. Our task is to create a program to find the maximum sum of pairs with specific difference in c++.

Problem Description − We will find pairs in such a way that the difference of elements of pairs is less than d. The sum of all such pairs should be maximum.

Let’s take an example to understand the problem,

Input

arr[] = {5, 9, 11, 7, 2, 12, 3} d = 5

Output

47

Explanation

Pairs that contribute to maximum sum: (3, 5), (7, 9), (11, 12). Sum = 3 + 5 + 7 + 9 + 11 + 12 = 47

Solution Approach

A simple and obvious solution to the problem is by creating all valid pairs of the arrays and then find the sum and return the maximum of all sums. But this solution is not efficient.

An efficient solution to the problem is by using a dynamic programming approach. Here, we will be finding optimum pairs that constitute a maximum sum. For this we will be using a sorted array, so first we will sort the given array and then operate on it. For finding the sum, we will use an array to store the maximum sum of pairs until the current element. For this, we will check if the current elements and the previous elements make a pair. If yes, we will add the pair sum to the maxSum till the array. Otherwise, the max sum will remain as it is.

Algorithm

Initialize: DP[n]

Step 1

For array arr[].

Step 2

DP[0] = 0;

Step 3

loop for i −> 1 to n

Step 3.1

check if pairs with the previous element is possible. if(arr[i]
− arr[i−1] < d).

Step 3.2

if Yes, check if the current pair sum results in a greater
value than the last considered sum and add the maximum value to the
current sum. i.e. if( (DP[i−2] + arr[i−1] + arr[i]) > (DP[i−1])) −>
DP[i] = (DP[i−2] + arr[i−1] + arr[i]), else −> DP[i] = DP[i−1].

Step 3.3

an exception is for value i = 1, where no value of DP[i−2] is
possible, in this case, DP[i−2] is not considered as it is the first pair
sum.

Step 4

Return DP[n−1].

Example

Program to illustrate the working of our solution,

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int CalcmaxPairSum(int arr[], int n, int d) {
   sort(arr, arr+n);
   int maxSumDP[n];
   maxSumDP[0] = 0;
   for (int i = 1; i < n; i++) {
      maxSumDP[i] = maxSumDP[i−1];
      if (arr[i] − arr[i−1] < d) {
         if (i >= 2)
         if(maxSumDP[i] < (maxSumDP[i−2] + arr[i−1] +
         arr[i]))
         maxSumDP[i] = (maxSumDP[i−2] + arr[i−1] +
         arr[i]);
         else
         if(maxSumDP[i] < (arr[i−1] + arr[i]))
         maxSumDP[i] = arr[i−1] + arr[i];
      }
   }
   return maxSumDP[n−1];
}
int main() {
   int arr[] = {5, 9, 11, 7, 2, 12, 3};
   int n = 7, d = 5;
   cout<<"The maximum sum of pairs with specific difference is "<<CalcmaxPairSum(arr, n, d);
   return 0;
}

Output

The maximum sum of pairs with specific difference is 47

Updated on: 09-Dec-2020

131 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements