
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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,
#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
- Related Articles
- Maximum sum of pairs with specific difference in C++
- Number of pairs with maximum sum in C++
- Python Program to print a specific number of rows with Maximum Sum
- Javascript Program to Count pairs with given sum
- Program to find two pairs of numbers where difference between sum of these pairs are minimized in python
- Program to find sum of contiguous sublist with maximum sum in Python
- Maximum count of pairs which generate the same sum in C++
- Python program to find sum of absolute difference between all pairs in a list
- Find Maximum difference between tuple pairs in Python
- Python Program to Filter Rows with a specific Pair Sum
- Print all the sum pairs which occur maximum number of times in C++
- Maximum Length Chain of Pairs
- Maximum sum of difference of adjacent elements in C++
- Count pairs with given sum in C++
- Maximum sum of absolute difference of any permutation in C++
