Maximum Count of pairs having equal Sum based on the given conditions
Given an array arr[] of length N containing array elements in the range [1, N], the task is to find the maximum number of pairs having equal sum, given that any element from the array can only be part of a single pair.
Examples:
Input: arr[] = {1, 4, 1, 4}
Output: 2
Explanation: Pairs {{1, 4}, {1, 4}} have equal sum 5.
Input: arr[] = {1, 2, 4, 3, 3, 5, 6}
Output: 3
Explanation: Pairs {{1, 5}, {2, 4}, {3, 3}} have equal sum 6.
Approach:
The sum of a pair that can be obtained from the array can neither be less than 2 times the minimum element of the array nor greater than 2 times the largest element, thus we find the maximum number of pairs that can be obtained for each of the sums lying between these extremities and output the maximum among these.
The approach to implement this is as follows:
- Store the frequencies of all the elements of the given array.
- Iterate through every sum lying between the extremities and count the maximum number of pairs we can obtain for each of these sums.
- Print the maximum count among all such pairs obtained to be giving the same summations.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxCount(vector< int >& freq, int mini, int maxi)
{
int n = freq.size() - 1;
int ans = 0;
for ( int sum = 2 * mini; sum <= 2 * maxi; ++sum) {
int possiblePair = 0;
for ( int firElement = 1; firElement < (sum + 1) / 2;
firElement++) {
if (sum - firElement <= maxi) {
possiblePair += min(freq[firElement],
freq[sum - firElement]);
}
}
if (sum % 2 == 0) {
possiblePair += freq[sum / 2] / 2;
}
ans = max(ans, possiblePair);
}
return ans;
}
int countofPairs(vector< int >& a)
{
int n = a.size();
int mini = *min_element(a.begin(), a.end()),
maxi = *max_element(a.begin(), a.end());
vector< int > freq(n + 1, 0);
for ( int i = 0; i < n; ++i)
freq[a[i]]++;
return maxCount(freq, mini, maxi);
}
int main()
{
vector< int > a = { 1, 2, 4, 3, 3, 5, 6 };
cout << countofPairs(a) << endl;
}
|
Java
import java.io.*;
class GFG{
static int maxCount( int [] freq, int maxi, int mini)
{
int n = freq.length - 1 ;
int ans = 0 ;
for ( int sum = 2 *mini; sum <= 2 * maxi; ++sum)
{
int possiblePair = 0 ;
for ( int firElement = 1 ;
firElement < (sum + 1 ) / 2 ;
firElement++)
{
if (sum - firElement <= maxi)
{
possiblePair += Math.min(freq[firElement],
freq[sum - firElement]);
}
}
if (sum % 2 == 0 )
{
possiblePair += freq[sum / 2 ] / 2 ;
}
ans = Math.max(ans, possiblePair);
}
return ans;
}
static int countofPairs( int [] a)
{
int n = a.length;
int []freq = new int [n + 1 ];
int maxi = - 1 ;
int mini = n+ 1 ;
for ( int i = 0 ;i<n;i++)
{
maxi = Math.max(maxi,a[i]);
mini = Math.min(mini,a[i]);
}
for ( int i = 0 ; i < n; ++i)
freq[a[i]]++;
return maxCount(freq,maxi,mini);
}
public static void main(String[] args)
{
int []a = { 1 , 2 , 4 , 3 , 3 , 5 , 6 };
System.out.print(countofPairs(a) + "\n" );
}
}
|
Python3
def maxCount(freq, maxi, mini):
n = len (freq) - 1
ans = 0
sum = 2 * mini
while sum < = 2 * maxi:
possiblePair = 0
for firElement in range ( 1 , ( sum + 1 ) / / 2 ):
if ( sum - firElement < = maxi):
possiblePair + = min (freq[firElement],
freq[ sum - firElement])
if ( sum % 2 = = 0 ):
possiblePair + = freq[ sum / / 2 ] / / 2
sum + = 1
ans = max (ans, possiblePair)
return ans
def countofPairs(a):
n = len (a)
freq = [ 0 ] * (n + 1 )
maxi = - 1
mini = n + 1
for i in range ( len (a)):
maxi = max (maxi, a[i])
mini = min (mini, a[i])
for i in range (n):
freq[a[i]] + = 1
return maxCount(freq, maxi, mini)
if __name__ = = "__main__" :
a = [ 1 , 2 , 4 , 3 , 3 , 5 , 6 ]
print (countofPairs(a))
|
C#
using System;
class GFG {
static int maxCount( int [] freq)
{
int n = freq.Length - 1;
int ans = 0;
for ( int sum = 2; sum <= 2 * n; ++sum) {
int possiblePair = 0;
for ( int firElement = 1;
firElement < (sum + 1) / 2; firElement++) {
if (sum - firElement <= n) {
possiblePair
+= Math.Min(freq[firElement],
freq[sum - firElement]);
}
}
if (sum % 2 == 0) {
possiblePair += freq[sum / 2] / 2;
}
ans = Math.Max(ans, possiblePair);
}
return ans;
}
static int countofPairs( int [] a)
{
int n = a.Length;
int [] freq = new int [n + 1];
for ( int i = 0; i < n; ++i)
freq[a[i]]++;
return maxCount(freq);
}
public static void Main(String[] args)
{
int [] a = { 1, 2, 4, 3, 3, 5, 6 };
Console.Write(countofPairs(a) + "\n" );
}
}
|
Javascript
<script>
function maxCount(freq, maxi, mini)
{
let n = freq.length - 1;
let ans = 0;
for (let sum = 2*mini; sum <= 2 * maxi; ++sum)
{
let possiblePair = 0;
for (let firElement = 1;
firElement < Math.floor((sum + 1) / 2);
firElement++)
{
if (sum - firElement <= maxi)
{
possiblePair += Math.min(freq[firElement],
freq[sum - firElement]);
}
}
if (sum % 2 == 0)
{
possiblePair += freq[sum / 2] / 2;
}
ans = Math.max(ans, possiblePair);
}
return ans;
}
function countofPairs(a)
{
let n = a.length;
let freq = Array.from({length: n+1}, (_, i) => 0);
let maxi = -1;
let mini = n+1;
for (let i = 0;i<n;i++)
{
maxi = Math.max(maxi,a[i]);
mini = Math.min(mini,a[i]);
}
for (let i = 0; i < n; ++i)
freq[a[i]]++;
return maxCount(freq,maxi,mini);
}
let a = [ 1, 2, 4, 3, 3, 5, 6 ];
document.write(countofPairs(a));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Last Updated :
12 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...