# Print all the sum pairs which occur maximum number of times

Given an array **arr[]** of **N** distinct integers. The task is to find the sum of two array integers **a[i] + a[j]** which occurs maximum number of times. In the case of multiple answers, print all of them.

**Examples:**

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

Output:

10

12

11

The sum 10, 12 and 11 occur 3 times

7 + 4 = 11, 8 + 3 = 11, 9 + 2 = 11

1 + 9 = 10, 8 + 2 = 10, 7 + 3 = 10

1 + 11 = 12, 8 + 4 = 12, 9 + 3 = 12

Input:arr[] = {3, 1, 7, 11, 9, 2, 12}

Output:

12

14

10

13

**Approach:** The following steps can be followed to solve the problem:

- Iterate over every pair of elements.
- Use a hash-table to count the number of times every sum pair occurs.
- At the end iterate over the hash-table and find the sum pair which occurs the maximum number of times.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the sum pairs ` `// that occur the most ` `void` `findSumPairs(` `int` `a[], ` `int` `n) ` `{ ` ` ` `// Hash-table ` ` ` `unordered_map<` `int` `, ` `int` `> mpp; ` ` ` `for` `(` `int` `i = 0; i < n - 1; i++) { ` ` ` `for` `(` `int` `j = i + 1; j < n; j++) { ` ` ` ` ` `// Keep a count of sum pairs ` ` ` `mpp[a[i] + a[j]]++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Variables to store ` ` ` `// maximum occurrence ` ` ` `int` `occur = 0; ` ` ` ` ` `// Iterate in the hash table ` ` ` `for` `(` `auto` `it : mpp) { ` ` ` `if` `(it.second > occur) { ` ` ` `occur = it.second; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print all sum pair which occur ` ` ` `// maximum number of times ` ` ` `for` `(` `auto` `it : mpp) { ` ` ` `if` `(it.second == occur) ` ` ` `cout << it.first << endl; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 1, 8, 3, 11, 4, 9, 2, 7 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` `findSumPairs(a, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

# Python 3 implementation of the approach

# Function to find the sum pairs

# that occur the most

def findSumPairs(a, n):

# Hash-table

mpp = {i:0 for i in range(21)}

for i in range(n – 1):

for j in range(i + 1, n, 1):

# Keep a count of sum pairs

mpp[a[i] + a[j]] += 1

# Variables to store

# maximum occurrence

occur = 0

# Iterate in the hash table

for key, value in mpp.items():

if (value > occur):

occur = value

# Print all sum pair which occur

# maximum number of times

for key, value in mpp.items():

if (value == occur):

print(key)

# Driver code

if __name__ == ‘__main__’:

a = [1, 8, 3, 11, 4, 9, 2, 7]

n = len(a)

findSumPairs(a, n)

# This code is contributed by

# Surendra_Gangwar

**Output:**

10 12 11

## Recommended Posts:

- Two odd occurring elements in an array where all other occur even times
- Unique element in an array where all elements occur k times except one
- Number of pairs with maximum sum
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times
- Find the maximum cost of an array of pairs choosing at most K pairs
- Print all pairs with given sum
- Find the maximum possible value of a[i] % a[j] over all pairs of i and j
- Print all pairs in an unsorted array with equal sum
- Maximum count of pairs which generate the same sum
- Maximum modulo of all the pairs of array where arr[i] >= arr[j]
- Maximum sum of pairs with specific difference
- Print all the pairs that contains the positive and negative values of an element
- Find the Number Occurring Odd Number of Times
- Minimum and Maximum sum of absolute differences of pairs
- Maximum subarray sum in array formed by repeating the given array k times

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.