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.
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; } |
6
Time Complexity: O(N * log(N) + M * log(M))
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Construct a Matrix with no element exceeding X and sum of two adjacent elements not exceeding Y
- Maximize count of pairs (i, j) from two arrays having element from first array not exceeding that from second array
- Count pairs with bitwise XOR exceeding bitwise AND from a given array
- Check if two items can be selected from two different categories without exceeding price
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K | Set 2
- Minimize count of Subsets with difference between maximum and minimum element not exceeding K
- Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K
- Count of all possible numbers not exceeding M having suffix N
- Count array elements exceeding sum of preceding K elements
- Count number of triangles possible with length of sides not exceeding N
- Count array elements with rank not exceeding K
- Count pairs from two sorted arrays whose sum is equal to a given value x
- Count pairs from two arrays having sum equal to K
- Count pairs from two arrays whose modulo operation yields K
- Count distinct pairs from two arrays having same sum of digits
- Given two arrays count all pairs whose sum is an odd number
- Count Pairs from two arrays with even sum
- Count of pairs between two arrays such that the sums are distinct
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

