# Number of pairs with maximum sum in C++

C++Server Side ProgrammingProgramming

In this tutorial, we are going to write a program that finds the count of pairs with maximum sum.

Let's see the steps to solve the problem.

• Iterate over the array with two loops.

• Find the maximum sum of pairs.

• Now, iterate over the array again and count the matching pairs.

• Return the pairs count.

## Example

Let's see the code.

#include <bits/stdc++.h>
using namespace std;
int getMaxSumPairsCount(int a[], int n) {
int maxSum = INT_MIN;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
maxSum = max(maxSum, a[i] + a[j]);
}
}
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] + a[j] == maxSum) {
count++;
}
}
}
return count;
}
int main() {
int arr[] = { 3, 4, 5, 2, 1, 2, 3, 4, 1, 5 };
int n = 10;
cout << getMaxSumPairsCount(arr, n) << endl;
return 0;
}

## Output

If you run the above code, then you will get the following result.

1

## Conclusion

If you have any queries in the tutorial, mention them in the comment section.

Published on 03-Jul-2021 08:33:33