Print all the sum pairs which occur maximum number of times | Java implementation of above 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
import java . util . * ; class GFG { static void findSumPairs ( int a [ ] , int n ) { Map < Integer , Integer > mpp = new HashMap < > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { mpp . put ( a [ i ] + a [ j ] , mpp . get ( a [ i ] + a [ j ] ) == null ? 1 : mpp . get ( a [ i ] + a [ j ] ) + 1 ) ; } } int occur = 0 ; for ( Map . Entry < Integer , Integer > entry : mpp . entrySet ( ) ) { if ( entry . getValue ( ) > occur ) { occur = entry . getValue ( ) ; } } for ( Map . Entry < Integer , Integer > entry : mpp . entrySet ( ) ) { if ( entry . getValue ( ) == occur ) System . out . println ( entry . getKey ( ) ) ; } } public static void main ( String args [ ] ) { int a [ ] = { 1 , 8 , 3 , 11 , 4 , 9 , 2 , 7 } ; int n = a . length ; findSumPairs ( a , n ) ; } }