Maximum count of pairs which generate the same sum
Last Updated :
01 Aug, 2022
Given an array arr[], the task is to count the maximum count of pairs that give the same sum.
Examples:
Input: arr[] = {1, 2, 3, 4}
Output: 2
(1, 2) = 3
(1, 3) = 4
(1, 4), (2 + 3) = 5
(2, 4) = 6
(3, 4) = 7
Input: arr[] = {1, 8, 3, 11, 4, 9, 2, 7}
Output: 3
Approach:
- Create a map to store the frequency of the sum of each pair.
- Traverse the map and find the maximum frequency.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxCountSameSUM( int arr[], int n)
{
unordered_map< int , int > M;
for ( int i = 0; i < n - 1; i++)
for ( int j = i + 1; j < n; j++)
M[(arr[i] + arr[j])]++;
int max_count = 0;
for ( auto ele : M)
if (max_count < ele.second)
max_count = ele.second;
return max_count;
}
int main()
{
int arr[] = { 1, 8, 3, 11, 4, 9, 2, 7 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << maxCountSameSUM(arr, n);
return 0;
}
|
Java
class GFG
{
static int Max( int arr[])
{
int max = arr[ 0 ];
for ( int i = 1 ; i < arr.length; i++)
if (arr[i] > max)max = arr[i];
return max;
}
static int maxCountSameSUM( int arr[], int n)
{
int maxi = Max(arr);
int [] M = new int [ 2 * maxi + 1 ];
for ( int i = 0 ; i < M.length; i++)M[i] = 0 ;
for ( int i = 0 ; i < n - 1 ; i++)
for ( int j = i + 1 ; j < n; j++)
M[(arr[i] + arr[j])] += 1 ;
int max_count = 0 ;
for ( int i = 0 ; i < 2 * maxi; i++)
if (max_count < M[i])
max_count = M[i];
return max_count;
}
public static void main(String args[])
{
int arr[] = { 1 , 8 , 3 , 11 , 4 , 9 , 2 , 7 };
int n = arr.length;
System.out.print(maxCountSameSUM(arr, n));
}
}
|
Python3
from collections import defaultdict
def maxCountSameSUM(arr, n):
M = defaultdict( lambda : 0 )
for i in range ( 0 , n - 1 ):
for j in range (i + 1 , n):
M[arr[i] + arr[j]] + = 1
max_count = 0
for ele in M:
if max_count < M[ele]:
max_count = M[ele]
return max_count
if __name__ = = "__main__" :
arr = [ 1 , 8 , 3 , 11 , 4 , 9 , 2 , 7 ]
n = len (arr)
print (maxCountSameSUM(arr, n))
|
C#
using System.Linq;
using System;
class GFG
{
static int maxCountSameSUM( int []arr, int n)
{
int maxi = arr.Max();
int [] M = new int [2 * maxi + 1];
for ( int i = 0; i < n - 1; i++)
for ( int j = i + 1; j < n; j++)
M[(arr[i] + arr[j])] += 1;
int max_count = 0;
for ( int i = 0; i < 2 * maxi; i++)
if (max_count < M[i])
max_count = M[i];
return max_count;
}
static void Main()
{
int []arr = { 1, 8, 3, 11, 4, 9, 2, 7 };
int n = arr.Length;
Console.WriteLine(maxCountSameSUM(arr, n));
}
}
|
PHP
<?php
function maxCountSameSUM( $arr , $n )
{
$maxi = max( $arr );
$M = array_fill (1, 2 * $maxi , 0);
for ( $i = 0; $i < $n - 1; $i ++)
for ( $j = $i + 1; $j < $n ; $j ++)
$M [( $arr [ $i ] + $arr [ $j ])] += 1;
$max_count = 0;
for ( $i = 0; $i < sizeof( $M ); $i ++)
if ( $max_count < $M [ $i ])
$max_count = $M [ $i ];
return $max_count ;
}
$arr = array ( 1, 8, 3, 11, 4, 9, 2, 7 );
$n = sizeof( $arr );
echo maxCountSameSUM( $arr , $n );
?>
|
Javascript
<script>
function maxCountSameSUM(arr, n)
{
var M = new Map();
for ( var i = 0; i < n - 1; i++)
for ( var j = i + 1; j < n; j++)
{
if (M.has(arr[i] + arr[j]))
M.set(arr[i] + arr[j], M.get(arr[i] + arr[j])+1)
else
M.set(arr[i] + arr[j], 1);
}
var max_count = 0;
M.forEach((value, key) => {
if (max_count < value)
max_count = value;
});
return max_count;
}
var arr = [1, 8, 3, 11, 4, 9, 2, 7];
var n = arr.length;
document.write( maxCountSameSUM(arr, n));
</script>
|
Time Complexity: O(n^2) as two loops are used to traverse the entire array.
Auxiliary Space: O(n), for storing counts of the sum of all pairs in the map
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...