# Maximum count of pairs which generate the same sum

Given an array arr[], the task is to count maximum count of pairs which give the same sum.

Examples:

Input: arr[] = {1, 2, 3, 4}
Output: 2
(1, 2) = 3
(1, 3) = 4
(1, 4), (2 + 3) = 5
(2, 4) = 6
(3, 4) = 7

Input: arr[] = {1, 8, 3, 11, 4, 9, 2, 7}
Output: 3

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. Create a map to store the frequency of the sum of each pair.
2. Traverse the map and find the maximum frequency.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the maximum ` `// count of pairs with equal sum ` `int` `maxCountSameSUM(``int` `arr[], ``int` `n) ` `{ ` `    ``// Create a map to store frequency ` `    ``unordered_map<``int``, ``int``> M; ` ` `  `    ``// Store counts of sum of all pairs ` `    ``// in the map ` `    ``for` `(``int` `i = 0; i < n - 1; i++) ` `        ``for` `(``int` `j = i + 1; j < n; j++) ` `            ``M[(arr[i] + arr[j])]++; ` ` `  `    ``int` `max_count = 0; ` ` `  `    ``// Find maximum count ` `    ``for` `(``auto` `ele : M) ` `        ``if` `(max_count < ele.second) ` `            ``max_count = ele.second; ` ` `  `    ``return` `max_count; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 8, 3, 11, 4, 9, 2, 7 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``cout << maxCountSameSUM(arr, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach  ` ` `  `class` `GFG  ` `{  ` `     `  `// return the max ` `static` `int` `Max(``int` `arr[]) ` `{ ` `    ``int` `max = arr[``0``]; ` `    ``for``(``int` `i = ``1``; i < arr.length; i++) ` `        ``if``(arr[i] > max)max = arr[i]; ` `     `  `    ``return` `max; ` `} ` `     `  `// Function to return the maximum  ` `// count of pairs with equal sum  ` `static` `int` `maxCountSameSUM(``int` `arr[], ``int` `n)  ` `{  ` `    ``int` `maxi = Max(arr);  ` `     `  `    ``// Create a map to store frequency  ` `    ``int``[] M = ``new` `int``[``2` `* maxi + ``1``];  ` `     `  `     `  `    ``for``(``int` `i = ``0``; i < M.length; i++)M[i] = ``0``; ` ` `  `    ``// Store counts of sum of all  ` `    ``// pairs in the map  ` `    ``for` `(``int` `i = ``0``; i < n - ``1``; i++)  ` `        ``for` `(``int` `j = i + ``1``; j < n; j++)  ` `            ``M[(arr[i] + arr[j])] += ``1``;  ` ` `  `    ``int` `max_count = ``0``;  ` ` `  `    ``// Find maximum count  ` `    ``for` `(``int` `i = ``0``; i < ``2` `* maxi; i++)  ` `        ``if` `(max_count < M[i])  ` `            ``max_count = M[i];  ` ` `  `    ``return` `max_count;  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `main(String args[])  ` `{  ` `    ``int` `arr[] = { ``1``, ``8``, ``3``, ``11``, ``4``, ``9``, ``2``, ``7` `};  ` `    ``int` `n = arr.length;  ` `    ``System.out.print(maxCountSameSUM(arr, n));  ` `}  ` `}  ` ` `  `// This code is contributed by Arnab Kundu `

## Python3

 `# Python3 implementation of the approach  ` `from` `collections ``import` `defaultdict ` ` `  `# Function to return the maximum  ` `# count of pairs with equal sum  ` `def` `maxCountSameSUM(arr, n):  ` ` `  `    ``# Create a map to store frequency  ` `    ``M ``=` `defaultdict(``lambda``:``0``) ` ` `  `    ``# Store counts of sum of  ` `    ``# all pairs in the map  ` `    ``for` `i ``in` `range``(``0``, n ``-` `1``):  ` `        ``for` `j ``in` `range``(i ``+` `1``, n):  ` `            ``M[arr[i] ``+` `arr[j]] ``+``=` `1` ` `  `    ``max_count ``=` `0` ` `  `    ``# Find maximum count  ` `    ``for` `ele ``in` `M: ` `        ``if` `max_count < M[ele]:  ` `            ``max_count ``=` `M[ele]  ` ` `  `    ``return` `max_count  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"``: ` ` `  `    ``arr ``=` `[``1``, ``8``, ``3``, ``11``, ``4``, ``9``, ``2``, ``7``]  ` `    ``n ``=` `len``(arr) ` `    ``print``(maxCountSameSUM(arr, n))  ` `     `  `# This code is contributed  ` `# by Rituraj Jain `

## C#

 `// C# implementation of the approach  ` `using` `System.Linq; ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// Function to return the maximum  ` `// count of pairs with equal sum  ` `static` `int` `maxCountSameSUM(``int` `[]arr, ``int` `n)  ` `{  ` `    ``int` `maxi = arr.Max(); ` `     `  `    ``// Create a map to store frequency  ` `    ``int``[] M = ``new` `int``[2 * maxi + 1];  ` ` `  `    ``// Store counts of sum of all  ` `    ``// pairs in the map  ` `    ``for` `(``int` `i = 0; i < n - 1; i++)  ` `        ``for` `(``int` `j = i + 1; j < n; j++)  ` `            ``M[(arr[i] + arr[j])] += 1;  ` ` `  `    ``int` `max_count = 0;  ` ` `  `    ``// Find maximum count  ` `    ``for` `(``int` `i = 0; i < 2 * maxi; i++) ` `        ``if` `(max_count < M[i])  ` `            ``max_count = M[i];  ` ` `  `    ``return` `max_count;  ` `}  ` ` `  `// Driver code ` `static` `void` `Main() ` `{ ` `    ``int` `[]arr = { 1, 8, 3, 11, 4, 9, 2, 7 };  ` `    ``int` `n = arr.Length;  ` `    ``Console.WriteLine(maxCountSameSUM(arr, n)); ` `} ` `} ` ` `  `// This code is contributed by mits `

## PHP

 ` `

Output:

```3
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.