Count pairs from two arrays with difference exceeding K
Last Updated :
05 Jul, 2023
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: The simplest approach to solve this problem is to traverse the array and generate all possible pairs of the given array and for each pair, check if (brr[j] – arr[i]) > K or not. If found to be true then increment the counter. Finally, print the value of the counter.
Algorithm
Initialize a variable sum to zero.
Loop through each element of the input array:
a. If the current element is even (i.e., its value is divisible by 2 with no remainder), add it to the sum.
Return the value of sum.
C++
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector< int > arr = {5, 9, 1, 8};
vector< int > brr = {10, 12, 7, 4, 2, 3};
int K = 3;
int count = 0;
for ( int i = 0; i < arr.size(); i++) {
for ( int j = 0; j < brr.size(); j++) {
if (brr[j] - arr[i] > K) {
count++;
}
}
}
cout << count << endl;
return 0;
}
|
Java
public class GFG {
public static void main(String[] args) {
int [] arr = { 5 , 9 , 1 , 8 };
int [] brr = { 10 , 12 , 7 , 4 , 2 , 3 };
int K = 3 ;
int count = 0 ;
for ( int i = 0 ; i < arr.length; i++) {
for ( int j = 0 ; j < brr.length; j++) {
if (brr[j] - arr[i] > K) {
count++;
}
}
}
System.out.println(count);
}
}
|
Python
arr = [ 5 , 9 , 1 , 8 ]
brr = [ 10 , 12 , 7 , 4 , 2 , 3 ]
K = 3
count = 0
for i in range ( len (arr)):
for j in range ( len (brr)):
if brr[j] - arr[i] > K:
count + = 1
print (count)
|
C#
using System;
public class GFG {
static public void Main() {
int [] arr = {5, 9, 1, 8};
int [] brr = {10, 12, 7, 4, 2, 3};
int K = 3;
int count = 0;
for ( int i = 0; i < arr.Length; i++) {
for ( int j = 0; j < brr.Length; j++) {
if (brr[j] - arr[i] > K) {
count++;
}
}
}
Console.WriteLine(count);
}
}
|
Javascript
function main() {
let arr = [5, 9, 1, 8];
let brr = [10, 12, 7, 4, 2, 3];
let K = 3;
let count = 0;
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < brr.length; j++) {
if (brr[j] - arr[i] > K) {
count++;
}
}
}
console.log(count);
}
main();
|
Time Complexity: O(N × M)
Auxiliary Space: O(1)
Efficient approach: To optimize the above approach the idea is to first sort the array and then use two pointer techniques. Follow the steps below to solve the problem:
- Initialize a variable, say cntPairs to store the count of pairs that satisfy the given conditions.
- Sort the given array.
- Initialize two variables, say i = 0 and j = 0 to store the index of left and right pointers respectively.
- Traverse both the array and check if (brr[j] – arr[i]) > K or not. If found to be true then update the value of cntPairs += (M – j) and increment the value of i pointer variable.
- Otherwise, increment the value of j pointer variable.
- Finally, print the value of cntPrint.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int count_pairs( int arr[], int brr[],
int N, int M, int K)
{
int i = 0;
int j = 0;
int cntPairs = 0;
sort(arr, arr + N);
sort(brr, brr + M);
while (i < N && j < M) {
if (brr[j] - arr[i] > K) {
cntPairs += (M - j);
i++;
}
else {
j++;
}
}
return cntPairs;
}
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 << count_pairs(arr, brr, N, M, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int count_pairs( int arr[], int brr[],
int N, int M, int K)
{
int i = 0 ;
int j = 0 ;
int cntPairs = 0 ;
Arrays.sort(arr);
Arrays.sort(brr);
while (i < N && j < M)
{
if (brr[j] - arr[i] > K)
{
cntPairs += (M - j);
i++;
}
else
{
j++;
}
}
return cntPairs;
}
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.println(count_pairs(arr, brr, N, M, K));
}
}
|
Python3
def count_pairs(arr, brr, N, M, K):
i = 0
j = 0
cntPairs = 0
arr = sorted (arr)
brr = sorted (brr)
while (i < N and j < M):
if (brr[j] - arr[i] > K):
cntPairs + = (M - j)
i + = 1
else :
j + = 1
return cntPairs
if __name__ = = '__main__' :
arr = [ 5 , 9 , 1 , 8 ]
brr = [ 10 , 12 , 7 , 4 , 2 , 3 ]
K = 3
N = len (arr)
M = len (brr)
print (count_pairs(arr, brr, N, M, K))
|
C#
using System;
class GFG{
static int count_pairs( int [] arr, int [] brr,
int N, int M, int K)
{
int i = 0;
int j = 0;
int cntPairs = 0;
Array.Sort(arr);
Array.Sort(brr);
while (i < N && j < M)
{
if (brr[j] - arr[i] > K)
{
cntPairs += (M - j);
i++;
}
else
{
j++;
}
}
return cntPairs;
}
static void Main()
{
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.WriteLine(
count_pairs(arr, brr,
N, M, K));
}
}
|
Javascript
<script>
function count_pairs(arr, brr, N, M, K)
{
let i = 0;
let j = 0;
let cntPairs = 0;
(arr).sort( function (a,b){ return a-b;});
(brr).sort( function (a,b){ return a-b;});
while (i < N && j < M)
{
if (brr[j] - arr[i] > K)
{
cntPairs += (M - j);
i++;
}
else
{
j++;
}
}
return cntPairs;
}
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(count_pairs(arr, brr, N, M, K));
</script>
|
Time Complexity: O(N * log(N) + M * log(M))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...