Count of equal value pairs from given two Arrays such that a[i] equals b[j]
Given two arrays a[] and b[] of length N and M respectively, sorted in non-decreasing order. The task is to find the number of pairs (i, j) such that, a[i] equals b[j].
Examples:
Input: a[] = {1, 1, 3, 3, 3, 5, 8, 8}, b[] = {1, 3, 3, 4, 5, 5, 5}
Output: 11
Explanation: Following are the 11 pairs with given condition The 11 pairs are {{1, 1}, {1, 1}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {5, 5}, {5, 5}, {5, 5}} .
Input: a[] = {1, 2, 3, 4}, b[] = {1, 1, 2}
Output: 3
Approach: This problem can be solved by using the Two Pointer approach. Let i point to the first element of array a[] and j point to the first element of array b[]. While traversing the arrays, there will be 3 cases.
Case 1: a[i] = b[j] Let target denote arr[i], cnt1 denote number of elements of array a that are equal to target and cnt2 denote the number of elements of array b that are equal to target. So the total number of pairs such that a[i] = b[j] is cnt1 * cnt2 . So our answer is incremented by cnt1 * cnt2 .
Case 2: a[i] < b[j] The only possibility of getting a[i] = b[j] in the future is by incrementing i, so we do i++.
Case 3: a[i] > b[j] The only possibility of getting a[i] = b[j] in the future is by incrementing j, so we do j++ .
Follow the steps below to solve the given problem.
- Initialize the variables ans, i and j as 0.
- Initialize answer, i, and j to 0 and start traversing both of the arrays till i is less than N or j is less than M.
- If a[i] equals b[j], calculate cnt1 and cnt2 and increment the answer by cnt1 * cnt2.
- If a[i] is less than b[j], increment i.
- If a[i] is greater than b[j], increment j.
- After performing the above steps, print the values of ans as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findPairs( int * a, int * b, int n, int m)
{
int ans = 0, i = 0, j = 0;
while (i < n && j < m) {
if (a[i] == b[j]) {
int target = a[i], cnt1 = 0, cnt2 = 0;
while (i < n && a[i] == target) {
cnt1++;
i++;
}
while (j < m && b[j] == target) {
cnt2++;
j++;
}
ans += (cnt1 * cnt2);
}
else if (a[i] < b[j])
i++;
else
j++;
}
return ans;
}
int main()
{
int N = 8, M = 7;
int a[] = { 1, 1, 3, 3, 3, 5, 8, 8 };
int b[] = { 1, 3, 3, 4, 5, 5, 5 };
cout << findPairs(a, b, N, M);
}
|
Java
import java.io.*;
class GFG{
static int findPairs( int [] a, int [] b, int n, int m)
{
int ans = 0 , i = 0 , j = 0 ;
while (i < n && j < m)
{
if (a[i] == b[j])
{
int target = a[i], cnt1 = 0 , cnt2 = 0 ;
while (i < n && a[i] == target)
{
cnt1++;
i++;
}
while (j < m && b[j] == target)
{
cnt2++;
j++;
}
ans += (cnt1 * cnt2);
}
else if (a[i] < b[j])
i++;
else
j++;
}
return ans;
}
public static void main(String[] args)
{
int N = 8 , M = 7 ;
int a[] = { 1 , 1 , 3 , 3 , 3 , 5 , 8 , 8 };
int b[] = { 1 , 3 , 3 , 4 , 5 , 5 , 5 };
System.out.println(findPairs(a, b, N, M));
}
}
|
Python3
def findPairs(a, b, n, m):
ans = 0
i = 0
j = 0
while (i < n and j < m):
if (a[i] = = b[j]):
target = a[i]
cnt1 = 0
cnt2 = 0
while (i < n and a[i] = = target):
cnt1 + = 1
i + = 1
while (j < m and b[j] = = target):
cnt2 + = 1
j + = 1
ans + = (cnt1 * cnt2)
elif (a[i] < b[j]):
i + = 1
else :
j + = 1
return ans
if __name__ = = "__main__" :
N = 8
M = 7
a = [ 1 , 1 , 3 , 3 , 3 , 5 , 8 , 8 ]
b = [ 1 , 3 , 3 , 4 , 5 , 5 , 5 ]
print (findPairs(a, b, N, M))
|
C#
using System;
class GFG{
static int findPairs( int [] a, int [] b, int n, int m)
{
int ans = 0, i = 0, j = 0;
while (i < n && j < m)
{
if (a[i] == b[j])
{
int target = a[i], cnt1 = 0, cnt2 = 0;
while (i < n && a[i] == target)
{
cnt1++;
i++;
}
while (j < m && b[j] == target)
{
cnt2++;
j++;
}
ans += (cnt1 * cnt2);
}
else if (a[i] < b[j])
i++;
else
j++;
}
return ans;
}
public static void Main()
{
int N = 8, M = 7;
int []a = { 1, 1, 3, 3, 3, 5, 8, 8 };
int []b = { 1, 3, 3, 4, 5, 5, 5 };
Console.Write(findPairs(a, b, N, M));
}
}
|
Javascript
<script>
function findPairs(a, b, n, m)
{
let ans = 0, i = 0, j = 0;
while (i < n && j < m) {
if (a[i] == b[j]) {
let target = a[i], cnt1 = 0, cnt2 = 0;
while (i < n && a[i] == target) {
cnt1++;
i++;
}
while (j < m && b[j] == target) {
cnt2++;
j++;
}
ans += (cnt1 * cnt2);
}
else if (a[i] < b[j])
i++;
else
j++;
}
return ans;
}
let N = 8, M = 7;
let a = [ 1, 1, 3, 3, 3, 5, 8, 8 ];
let b = [ 1, 3, 3, 4, 5, 5, 5 ];
document.write(findPairs(a, b, N, M));
</script>
|
Time Complexity: O(N + M)
Auxiliary Space: O(1)
Last Updated :
13 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...