Count of pairs between two arrays such that the sums are distinct
Given two arrays a[] and b[], the task is to find the count of all pairs (a[i], b[j]) such that a[i] + b[j] is unique among all the pairs i.e. if two pairs have equal sum then only one will be counted in the result.
Examples:
Input: a[] = {3, 3}, b[] = {3}
Output: 1
The two possible pairs are (a[0], b[0]) and (a[1], b[0]).
Pair 1: 3 + 3 = 6
Pair 2: 3 + 3 = 6
Input: a[] = {12, 2, 7}, b[] = {4, 3, 8}
Output: 7
Approach: Initialise count = 0 and run two loops to consider all possible pairs and store the sum of every pair in an unordered_set to check whether the sum has been obtained before. If it has then ignore the current pair else increment the count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countPairs( int a[], int b[], int n, int m)
{
int cnt = 0;
unordered_set< int > s;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
int sum = a[i] + b[j];
if (s.count(sum) == 0) {
cnt++;
s.insert(sum);
}
}
}
return cnt;
}
int main()
{
int a[] = { 12, 2, 7 };
int n = sizeof (a) / sizeof (a[0]);
int b[] = { 4, 3, 8 };
int m = sizeof (b) / sizeof (b[0]);
cout << countPairs(a, b, n, m);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int countPairs( int a[], int b[], int n, int m)
{
int cnt = 0 ;
HashSet<Integer> s = new HashSet<Integer>();
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < m; j++)
{
int sum = a[i] + b[j];
if (s.contains(sum) == false )
{
cnt++;
s.add(sum);
}
}
}
return cnt;
}
static public void main (String args[])
{
int a[] = { 12 , 2 , 7 };
int n = a.length;
int b[] = { 4 , 3 , 8 };
int m = b.length;
System.out.println(countPairs(a, b, n, m));
}
}
|
Python3
def countPairs(a, b, n, m):
cnt = 0
s = dict ()
for i in range (n):
for j in range (m):
sum = a[i] + b[j]
if ( sum not in s.keys()):
cnt + = 1
s[ sum ] = 1
return cnt
a = [ 12 , 2 , 7 ]
n = len (a)
b = [ 4 , 3 , 8 ]
m = len (b)
print (countPairs(a, b, n, m))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int countPairs( int []a, int []b,
int n, int m)
{
int cnt = 0;
HashSet< int > s = new HashSet< int >();
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < m; j++)
{
int sum = a[i] + b[j];
if (s.Contains(sum) == false )
{
cnt++;
s.Add(sum);
}
}
}
return cnt;
}
static public void Main (String []args)
{
int []a = { 12, 2, 7 };
int n = a.Length;
int []b = { 4, 3, 8 };
int m = b.Length;
Console.WriteLine(countPairs(a, b, n, m));
}
}
|
Javascript
<script>
function countPairs(a, b, n, m)
{
let cnt = 0;
let s = new Set();
for (let i = 0; i < n; i++)
{
for (let j = 0; j < m; j++)
{
let sum = a[i] + b[j];
if (s.has(sum) == false )
{
cnt++;
s.add(sum);
}
}
}
return cnt;
}
let a = [ 12, 2, 7 ];
let n = a.length;
let b = [ 4, 3, 8 ];
let m = b.length;
document.write(countPairs(a, b, n, m));
</script>
|
Time complexity: O(N * M).
Auxiliary Space: O(1).
Last Updated :
14 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...