合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# 最大和的对数 > 原文: [https://www.geeksforgeeks.org/number-pairs-maximum-sum/](https://www.geeksforgeeks.org/number-pairs-maximum-sum/) 给定一个数组 arr [],对对 arr [i],arr [j]的数目为 arr [i] + arr [j]最大,且 i < j 为对。 ``` Example : Input : arr[] = {1, 1, 1, 2, 2, 2} Output : 3 Explanation: The maximum possible pair sum where i<j is 4, which is given by 3 pairs, so the answer is 3 the pairs are (2, 2), (2, 2) and (2, 2) Input : arr[] = {1, 4, 3, 3, 5, 1} Output : 1 Explanation: The pair 4, 5 yields the maximum sum i.e, 9 which is given by 1 pair only ``` **方法 1(朴素)** 从 0 到 n 遍历一个循环 i,即数组的长度,从 i + 1 到 n 遍历另一个循环 j,以找到与 i 的所有可能的对< j。 找到具有最大可能总和的货币对,再次遍历所有货币对,并保持货币对数量的计数,这使货币对总和等于最大值 ## C++ ```cpp // CPP program to count pairs with maximum sum. #include <bits/stdc++.h> using namespace std; // function to find the number of maximum pair sums int sum(int a[], int n) {     // traverse through all the pairs     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]);     // traverse through all pairs and keep a count     // of the number of maximum pairs     int c = 0;     for (int i = 0; i < n; i++)         for (int j = i + 1; j < n; j++)             if (a[i] + a[j] == maxSum)                 c++;     return c; } // driver program to test the above function int main() {     int array[] = { 1, 1, 1, 2, 2, 2 };     int n = sizeof(array) / sizeof(array[0]);     cout << sum(array, n);     return 0; } ```