Print All Sum Pairs Occurring Maximum Times in C++



In this problem, we are given an array of n unique integers. And we have to find the sum of two integers of the array which has the maximum frequency. The problem has multiple solutions and you need to find them all.

Input : array = { 1, 12, 5, 7, 9, 11}
Output : 16 12

Explanation − sum 16 and 12 occur two times.

5 + 11 = 16 & 7 + 9 = 16
1 + 11 = 12 & 5 + 7 = 12

Now to solve this problem, our approach to the problem is checking the occurrence every sum pair and then print the pair with the maximum number of times.

Steps to solve the problem

Step 1: Iterate over all pairs.
Step 2: The occurrence of sum pairs is counted using hash-table.
Step 3: After the interation process is done, the sum pair with maximum occurrence is printed.

Example

#include 
using namespace std;
void sumPairs(int a[], int n){
   unordered_map pairSum;
   for (int i = 0; i  occur) {
         occur = it.second;
      }
   }
   for (auto it : pairSum) {
      if (it.second == occur)
         cout 

Output

The sum pairs with max occurrence are −

16 12
Updated on: 2020-01-17T09:42:13+05:30

146 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements