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