Count pairs from two arrays with difference exceeding K | set 2
Last Updated :
23 Apr, 2021
Given two integer arrays arr[] and brr[] consisting of distinct elements of size N and M respectively and an integer K, the task is to find the count of pairs (arr[i], brr[j]) such that (brr[j] – arr[i]) > K.
Examples:
Input: arr[] = {5, 9, 1, 8}, brr[] = {10, 12, 7, 4, 2, 3}, K = 3
Output: 6
Explanation:
Possible pairs that satisfy the given conditions are: {(5, 10), (5, 12), (1, 10), (1, 12), (1, 7), (8, 12)}.
Therefore, the required output is 6.
Input: arr[] = {2, 10}, brr[] = {5, 7}, K = 2
Output: 2
Explanation:
Possible pairs that satisfy the given conditions are: {(2, 5), (2, 7)}.
Therefore, the required output is 2.
Naive Approach: Refer to the previous post of this article for the simplest approach to solve this problem.
Time Complexity: O(N * M)
Auxiliary Space: O(1)
Two Pointer Approach: Refer to the previous post of this article for the two pointer solution to this problem.
Time Complexity: O(N * log(N) + M * log(M))
Auxiliary Space: O(1)
Efficient Approach: The idea is to sort the smaller array and traverse through the larger array for each element, find the appropriate element in the smaller array for pairing using Binary Search. Follow the steps below to solve the problem:
- Initialize a variable count to store the count of pairs.
- If arr[] size is smaller than brr[], then do the following.
- Sort the array arr[] and traverse the array brr[].
- Find the lower bound of (brr[j] – k) in arr[] since the numbers which are less than brr[j] – k in arr[] will perfectly make pair with brr[j].
- Add the lower bound index with the count.
- If brr[] size is smaller than arr[], then do the following.
- Sort the array brr[] and traverse through arr[].
- Find the upper bound of (arr[i] + k) in brr[] since the numbers which are greater than arr[i]+k in brr[] will perfectly make pair with arr[i].
- Add (Size of brr[] – upper bound index) with the count.
- After the above steps, print the value of count as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countPairs( int v1[], int v2[],
int n, int m, int k)
{
int count = 0;
if (n <= m) {
sort(v1, v1 + n);
for ( int j = 0; j < m; j++) {
int index
= lower_bound(v1, v1 + n,
v2[j] - k)
- v1;
count += index;
}
}
else {
sort(v2, v2 + m);
for ( int i = 0; i < n; i++) {
int index
= upper_bound(v2, v2 + m,
v1[i] + k)
- v2;
count += m - index;
}
}
return count;
}
int main()
{
int arr[] = { 5, 9, 1, 8 };
int brr[] = { 10, 12, 7, 4, 2, 3 };
int K = 3;
int N = sizeof (arr)
/ sizeof (arr[0]);
int M = sizeof (brr)
/ sizeof (brr[0]);
cout << countPairs(arr, brr,
N, M, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int countPairs( int v1[], int v2[],
int n, int m, int k)
{
int count = 0 ;
if (n <= m)
{
Arrays.sort(v1);
for ( int j = 0 ; j < m; j++)
{
int index = lowerBound(v1, 0 , n,
v2[j] - k);
count += index;
}
}
else
{
Arrays.sort(v2);
for ( int i = 0 ; i < n; i++)
{
int index = upperBound(v2, 0 , m,
v1[i] + k);
count += m - index;
}
}
return count;
}
static int lowerBound( int [] a, int low,
int high, int element)
{
while (low < high)
{
int middle = low +
(high - low) / 2 ;
if (element > a[middle])
low = middle + 1 ;
else
high = middle;
}
return low;
}
static int upperBound( int [] a, int low,
int high, int element)
{
while (low < high)
{
int middle = low +
(high - low) / 2 ;
if (a[middle] > element)
high = middle;
else
low = middle + 1 ;
}
return low;
}
public static void main(String[] args)
{
int arr[] = { 5 , 9 , 1 , 8 };
int brr[] = { 10 , 12 , 7 ,
4 , 2 , 3 };
int K = 3 ;
int N = arr.length;
int M = brr.length;
System.out.print(countPairs(arr, brr,
N, M, K));
}
}
|
Python3
from bisect import bisect_left, bisect_right
def countPairs(v1, v2, n, m, k):
count = 0
if (n < = m):
v1 = sorted (v1)
for j in range (m):
index = bisect_left(v1, v2[j] - k)
count + = index
else :
v2 = sorted (v2)
for i in range (n):
index = bisect_right(v2, v1[i] + k)
count + = m - index
return count
if __name__ = = '__main__' :
arr = [ 5 , 9 , 1 , 8 ]
brr = [ 10 , 12 , 7 , 4 , 2 , 3 ]
K = 3
N = len (arr)
M = len (brr)
print (countPairs(arr, brr, N, M, K))
|
C#
using System;
class GFG{
static int countPairs( int []v1, int []v2,
int n, int m, int k)
{
int count = 0;
if (n <= m)
{
Array.Sort(v1);
for ( int j = 0; j < m; j++)
{
int index = lowerBound(v1, 0, n,
v2[j] - k);
count += index;
}
}
else
{
Array.Sort(v2);
for ( int i = 0; i < n; i++)
{
int index = upperBound(v2, 0, m,
v1[i] + k);
count += m - index;
}
}
return count;
}
static int lowerBound( int [] a, int low,
int high, int element)
{
while (low < high)
{
int middle = low +
(high - low) / 2;
if (element > a[middle])
low = middle + 1;
else
high = middle;
}
return low;
}
static int upperBound( int [] a, int low,
int high, int element)
{
while (low < high)
{
int middle = low +
(high - low) / 2;
if (a[middle] > element)
high = middle;
else
low = middle + 1;
}
return low;
}
public static void Main(String[] args)
{
int []arr = {5, 9, 1, 8};
int []brr = {10, 12, 7,
4, 2, 3};
int K = 3;
int N = arr.Length;
int M = brr.Length;
Console.Write(countPairs(arr, brr,
N, M, K));
}
}
|
Javascript
<script>
function countPairs(v1, v2, n, m, k)
{
let count = 0;
if (n <= m)
{
v1.sort();
for (let j = 0; j < m; j++)
{
let index = lowerBound(v1, 0, n,
v2[j] - k);
count += index;
}
}
else
{
v2.sort();
for (let i = 0; i < n; i++)
{
let index = upperBound(v2, 0, m,
v1[i] + k);
count += m - index;
}
}
return count;
}
function lowerBound(a, low,
high, element)
{
while (low < high)
{
let middle = low +
(high - low) / 2;
if (element > a[middle])
low = middle + 1;
else
high = middle;
}
return low;
}
function upperBound(a, low,
high, element)
{
while (low < high)
{
let middle = low +
(high - low) / 2;
if (a[middle] > element)
high = middle;
else
low = middle + 1;
}
return low;
}
let arr = [5, 9, 1, 8];
let brr = [10, 12, 7,
4, 2, 3];
let K = 3;
let N = arr.length;
let M = brr.length;
document.write(countPairs(arr, brr,
N, M, K));
</script>
|
Time Complexity: O((N+M) * min(log(N), log(M)))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...