Print all the sum pairs which occur maximum number of times
Last Updated :
16 Jun, 2022
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++
#include <bits/stdc++.h>
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;
}
|
Java
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);
}
}
|
Python3
def findSumPairs(a, n):
mpp = {i: 0 for i in range ( 21 )}
for i in range (n - 1 ):
for j in range (i + 1 , n, 1 ):
mpp[a[i] + a[j]] + = 1
occur = 0
for key, value in mpp.items():
if (value > occur):
occur = value
for key, value in mpp.items():
if (value = = occur):
print (key)
if __name__ = = '__main__' :
a = [ 1 , 8 , 3 , 11 , 4 , 9 , 2 , 7 ]
n = len (a)
findSumPairs(a, n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void findSumPairs( int []a, int n)
{
Dictionary< int , int > mpp = new Dictionary< int , int >();
for ( int i = 0; i < n - 1; i++)
{
for ( int j = i + 1; j < n; j++)
{
if (mpp.ContainsKey(a[i] + a[j]))
{
var val = mpp[a[i] + a[j]];
mpp.Remove(a[i] + a[j]);
mpp.Add(a[i] + a[j], val + 1);
}
else
{
mpp.Add(a[i] + a[j], 1);
}
}
}
int occur = 0;
foreach (KeyValuePair< int , int > entry in mpp)
{
if (entry.Value > occur)
{
occur = entry.Value;
}
}
foreach (KeyValuePair< int , int > entry in mpp)
{
if (entry.Value == occur)
Console.WriteLine(entry.Key);
}
}
public static void Main(String []args)
{
int []a = { 1, 8, 3, 11, 4, 9, 2, 7 };
int n = a.Length;
findSumPairs(a, n);
}
}
|
Javascript
<script>
function findSumPairs( a, n){
let mpp = new Map();
for (let i = 0; i < n - 1; i++) {
for (let j = i + 1; j < n; j++) {
if (mpp[a[i]+a[j]])
mpp[a[i]+a[j]]++;
else
mpp[a[i]+a[j]] = 1;
}
}
let occur = 0;
for ( var it in mpp) {
if (mpp[it] > occur) {
occur = mpp[it];
}
}
for ( var it in mpp) {
if (mpp[it] == occur)
document.write( it , '<br>' );
}
}
let a = [ 1, 8, 3, 11, 4, 9, 2, 7 ];
let len = a.length;
findSumPairs(a, len);
</script>
|
Time Complexity: O(N*N), as we are using nested loops for traversing N*N times. Where N is the number of elements in the array.
Auxiliary Space: O(N*N), as we are using extra space for the map. Where N is the number of elements in the array.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...