Count pairs (i, j) from arrays arr[] & brr[] such that arr[i] – brr[j] = arr[j] – brr[i]
Given two arrays arr[] and brr[] consisting of N integers, the task is to count the number of pairs (i, j) from both the array such that (arr[i] – brr[j]) and (arr[j] – brr[i]) are equal.
Examples:
Input: A[] = {1, 2, 3, 2, 1}, B[] = {1, 2, 3, 2, 1}
Output: 2
Explanation: The pairs satisfying the condition are:
- (1, 5): arr[1] – brr[5] = 1 – 1 = 0, arr[5[ – brr[1] = 1 – 1 = 0
- (2, 4): arr[2] – brr[4] = 2 – 2 = 0, arr[4] – brr[2] = 2 – 2 = 0
Input: A[] = {1, 4, 20, 3, 10, 5}, B[] = {9, 6, 1, 7, 11, 6}
Output: 4
Naive Approach: The simplest approach to solve the problem is to generate all pairs from two given arrays and check for the required condition. For every pair for which the condition is found to be true, increase count of such pairs. Finally, print the count obtained.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The idea is to transform the given expression (a[i] – b[j] = a[j] – b[i]) into the form (a[i] + b[i] = a[j] + b[j]) and then calculate pairs satisfying the condition. Below are the steps:
- Transform the expression, a[i] – b[j] = a[j] – b[i] ==> a[i] + b[i] = a[j] +b[j]. The general form of expression becomes to count the sum of values at each corresponding index of the two arrays for any pair (i, j).
- Initialize an auxiliary array c[] to store the corresponding sum c[i] = a[i] + b[i] at each index i.
- Now the problem reduces to find the number of possible pairs having same c[i] value.
- Count the frequency of each element in the array c[] and If any c[i] frequency value is greater than one then it can make a pair.
- Count the number of valid pairs in the above steps using formula:
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 C[n];
for ( int i = 0; i < n; i++) {
C[i] = a[i] + b[i];
}
map< int , int > freqCount;
for ( int i = 0; i < n; i++) {
freqCount[C[i]]++;
}
int NoOfPairs = 0;
for ( auto x : freqCount) {
int y = x.second;
NoOfPairs = NoOfPairs
+ y * (y - 1) / 2;
}
cout << NoOfPairs;
}
int main()
{
int arr[] = { 1, 4, 20, 3, 10, 5 };
int brr[] = { 9, 6, 1, 7, 11, 6 };
int N = sizeof (arr) / sizeof (arr[0]);
CountPairs(arr, brr, N);
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class GFG{
static void CountPairs( int a[], int b[], int n)
{
int C[] = new int [n];
for ( int i = 0 ; i < n; i++)
{
C[i] = a[i] + b[i];
}
HashMap<Integer,
Integer> freqCount = new HashMap<>();
for ( int i = 0 ; i < n; i++)
{
if (!freqCount.containsKey(C[i]))
freqCount.put(C[i], 1 );
else
freqCount.put(C[i],
freqCount.get(C[i]) + 1 );
}
int NoOfPairs = 0 ;
for (Map.Entry<Integer,
Integer> x : freqCount.entrySet())
{
int y = x.getValue();
NoOfPairs = NoOfPairs +
y * (y - 1 ) / 2 ;
}
System.out.println(NoOfPairs);
}
public static void main(String args[])
{
int arr[] = { 1 , 4 , 20 , 3 , 10 , 5 };
int brr[] = { 9 , 6 , 1 , 7 , 11 , 6 };
int N = arr.length;
CountPairs(arr, brr, N);
}
}
|
Python3
def CountPairs(a, b, n):
C = [ 0 ] * n
for i in range (n):
C[i] = a[i] + b[i]
freqCount = dict ()
for i in range (n):
if C[i] in freqCount.keys():
freqCount[C[i]] + = 1
else :
freqCount[C[i]] = 1
NoOfPairs = 0
for x in freqCount:
y = freqCount[x]
NoOfPairs = (NoOfPairs + y *
(y - 1 ) / / 2 )
print (NoOfPairs)
arr = [ 1 , 4 , 20 , 3 , 10 , 5 ]
brr = [ 9 , 6 , 1 , 7 , 11 , 6 ]
N = len (arr)
CountPairs(arr, brr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void CountPairs( int []a, int []b,
int n)
{
int []C = new int [n];
for ( int i = 0; i < n; i++)
{
C[i] = a[i] + b[i];
}
Dictionary< int ,
int > freqCount = new Dictionary< int ,
int >();
for ( int i = 0; i < n; i++)
{
if (!freqCount.ContainsKey(C[i]))
freqCount.Add(C[i], 1);
else
freqCount[C[i]] = freqCount[C[i]] + 1;
}
int NoOfPairs = 0;
foreach (KeyValuePair< int ,
int > x in freqCount)
{
int y = x.Value;
NoOfPairs = NoOfPairs +
y * (y - 1) / 2;
}
Console.WriteLine(NoOfPairs);
}
public static void Main(String []args)
{
int []arr = { 1, 4, 20, 3, 10, 5 };
int []brr = { 9, 6, 1, 7, 11, 6 };
int N = arr.Length;
CountPairs(arr, brr, N);
}
}
|
Javascript
<script>
function CountPairs(a,b, n)
{
var C = Array(n);
for ( var i = 0; i < n; i++) {
C[i] = a[i] + b[i];
}
var freqCount = new Map();
for ( var i = 0; i < n; i++) {
if (freqCount.has(C[i]))
freqCount.set(C[i], freqCount.get(C[i])+1)
else
freqCount.set(C[i], 1)
}
var NoOfPairs = 0;
freqCount.forEach((value, key) => {
var y = value;
NoOfPairs = NoOfPairs
+ y * (y - 1) / 2;
});
document.write( NoOfPairs);
}
var arr = [1, 4, 20, 3, 10, 5];
var brr = [ 9, 6, 1, 7, 11, 6 ];
var N = arr.length;
CountPairs(arr, brr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
17 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...