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;
// Traverse the array and generate all possible pairs
for (int i = 0; i < arr.size(); i++) {
for (int j = 0; j < brr.size(); j++) {
// Check if (brr[j] - arr[i]) > K
if (brr[j] - arr[i] > K) {
// Increment the counter
count++;
}
}
}
// Print the 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;
// Traverse the array and generate all possible pairs
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < brr.length; j++) {
// Check if (brr[j] - arr[i]) > K
if (brr[j] - arr[i] > K) {
// Increment the counter
count++;
}
}
}
// Print the count
System.out.println(count);
}
}
Python
arr = [5, 9, 1, 8]
brr = [10, 12, 7, 4, 2, 3]
K = 3
count = 0
# Traverse the array and generate all possible pairs
for i in range(len(arr)):
for j in range(len(brr)):
# Check if (brr[j] - arr[i]) > K
if brr[j] - arr[i] > K:
# Increment the counter
count += 1
# Print the count
print(count)
C#
// C# code for above approach
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;
// Traverse the array and generate all possible pairs
for (int i = 0; i < arr.Length; i++) {
for (int j = 0; j < brr.Length; j++) {
// Check if (brr[j] - arr[i]) > K
if (brr[j] - arr[i] > K) {
// Increment the counter
count++;
}
}
}
// Print the count
Console.WriteLine(count);
}
}
// This code is contributed by Utkarsh Kumar
JavaScript
function main() {
let arr = [5, 9, 1, 8];
let brr = [10, 12, 7, 4, 2, 3];
let K = 3;
let count = 0;
// Traverse the array and generate all possible pairs
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < brr.length; j++) {
// Check if (brr[j] - arr[i]) > K
if (brr[j] - arr[i] > K) {
// Increment the counter
count++;
}
}
}
// Print the count
console.log(count);
}
main();
// This code is contributed by shivhack999
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++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count pairs that satisfy
// the given conditions
int count_pairs(int arr[], int brr[],
int N, int M, int K)
{
// Stores index of
// the left pointer.
int i = 0;
// Stores index of
// the right pointer
int j = 0;
// Stores count of total pairs
// that satisfy the conditions
int cntPairs = 0;
// Sort arr[] array
sort(arr, arr + N);
// Sort brr[] array
sort(brr, brr + M);
// Traverse both the array
// and count then pairs
while (i < N && j < M) {
// If the value of
// (brr[j] - arr[i]) exceeds K
if (brr[j] - arr[i] > K) {
// Update cntPairs
cntPairs += (M - j);
// Update
i++;
}
else {
// Update j
j++;
}
}
return cntPairs;
}
// Driver Code
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
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to count pairs that satisfy
// the given conditions
static int count_pairs(int arr[], int brr[],
int N, int M, int K)
{
// Stores index of
// the left pointer.
int i = 0;
// Stores index of
// the right pointer
int j = 0;
// Stores count of total pairs
// that satisfy the conditions
int cntPairs = 0;
// Sort arr[] array
Arrays.sort(arr);
// Sort brr[] array
Arrays.sort(brr);
// Traverse both the array
// and count then pairs
while (i < N && j < M)
{
// If the value of
// (brr[j] - arr[i]) exceeds K
if (brr[j] - arr[i] > K)
{
// Update cntPairs
cntPairs += (M - j);
// Update
i++;
}
else
{
// Update j
j++;
}
}
return cntPairs;
}
// Driver Code
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));
}
}
// This code is contributed by SURENDRA_GANGWAR
Python3
# Python3 program to implement
# the above approach
# Function to count pairs that satisfy
# the given conditions
def count_pairs(arr, brr, N, M, K):
# Stores index of
# the left pointer.
i = 0
# Stores index of
# the right pointer
j = 0
# Stores count of total pairs
# that satisfy the conditions
cntPairs = 0
# Sort arr[] array
arr = sorted(arr)
# Sort brr[] array
brr = sorted(brr)
# Traverse both the array
# and count then pairs
while (i < N and j < M):
# If the value of
# (brr[j] - arr[i]) exceeds K
if (brr[j] - arr[i] > K):
# Update cntPairs
cntPairs += (M - j)
# Update
i += 1
else:
# Update j
j += 1
return cntPairs
# Driver Code
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))
# This code is contributed by mohit kumar 29
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to count pairs
// that satisfy the given
// conditions
static int count_pairs(int[] arr, int[] brr,
int N, int M, int K)
{
// Stores index of
// the left pointer.
int i = 0;
// Stores index of
// the right pointer
int j = 0;
// Stores count of total pairs
// that satisfy the conditions
int cntPairs = 0;
// Sort arr[] array
Array.Sort(arr);
// Sort brr[] array
Array.Sort(brr);
// Traverse both the array
// and count then pairs
while (i < N && j < M)
{
// If the value of
// (brr[j] - arr[i])
// exceeds K
if (brr[j] - arr[i] > K)
{
// Update cntPairs
cntPairs += (M - j);
// Update
i++;
}
else
{
// Update j
j++;
}
}
return cntPairs;
}
// Driver code
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));
}
}
// This code is contributed by divyeshrabadiya07
JavaScript
<script>
// Javascript program to implement
// the above approach
// Function to count pairs that satisfy
// the given conditions
function count_pairs(arr, brr, N, M, K)
{
// Stores index of
// the left pointer.
let i = 0;
// Stores index of
// the right pointer
let j = 0;
// Stores count of total pairs
// that satisfy the conditions
let cntPairs = 0;
// Sort arr[] array
(arr).sort(function(a,b){return a-b;});
// Sort brr[] array
(brr).sort(function(a,b){return a-b;});
// Traverse both the array
// and count then pairs
while (i < N && j < M)
{
// If the value of
// (brr[j] - arr[i]) exceeds K
if (brr[j] - arr[i] > K)
{
// Update cntPairs
cntPairs += (M - j);
// Update
i++;
}
else
{
// Update j
j++;
}
}
return cntPairs;
}
// Driver Code
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));
// This code is contributed by unknown2108
</script>
Time Complexity: O(N * log(N) + M * log(M))
Auxiliary Space: O(1)
Similar Reads
Count pairs from two arrays with difference exceeding K | set 2 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 = 3Output: 6Expla
10 min read
Count pairs with absolute difference equal to k Given an array arr[] and a positive integer k, the task is to count all pairs (i, j) such that i < j and absolute value of (arr[i] - arr[j]) is equal to k. Examples: Input: arr[] = [1, 4, 1, 4, 5], k = 3Output: 4Explanation: There are 4 pairs with absolute difference 3, the pairs are [1, 4], [1,
15+ min read
Count all distinct pairs with difference equal to K | Set 2 Given an integer array arr[] and a positive integer K, the task is to count all distinct pairs with differences equal to K. Examples: Input: arr[ ] = {1, 5, 3, 4, 2}, K = 3Output: 2Explanation: There are 2 distinct pairs with difference 3, the pairs are {1, 4} and {5, 2} Input: arr[] = {8, 12, 16, 4
6 min read
Count all pairs of an array which differ in K bits Given an array of size n and integer k, count all pairs in array which differ in exactly K bits of binary representation of both the numbers.The input arrays have elements with small values and possibly many repetitions. Examples: Input: arr[] = {2, 4, 1, 3, 1} k = 2 Output: 5 Explanation: There are
14 min read
Count pairs from given array with Bitwise OR equal to K Given an array arr[] consisting of N positive integers and an integer K, the task is to count all pairs possible from the given array with Bitwise OR equal to K. Examples: Input: arr[] = {2, 38, 44, 29, 62}, K = 46Output: 2Explanation: Only the following two pairs are present in the array whose Bitw
5 min read
Count pairs from two arrays having sum equal to K Given an integer K and two arrays A1 and A2, the task is to return the total number of pairs (one element from A1 and one element from A2) with a sum equal to K. Note: Arrays can have duplicate elements. We consider every pair as different, the only constraint is, an element (of any array) can parti
6 min read