Count pairs in an array such that the absolute difference between them is ≥ K
Given an array arr[] and an integer K, the task is to find the count of pairs (arr[i], arr[j]) from the array such that |arr[i] – arr[j]| ≥ K. Note that (arr[i], arr[j]) and arr[j], arr[i] will be counted only once.
Examples:
Input: arr[] = {1, 2, 3, 4}, K = 2
Output: 3
All valid pairs are (1, 3), (1, 4) and (2, 4)Input: arr[] = {7, 4, 12, 56, 123}, K = 50
Output: 5
Approach: Sort the given array. Now for every element arr[i], find the first element on the right arr[j] such that (arr[j] – arr[i]) ≥ K. This is because after this element, every element will satisfy the same condition with arr[i] as the array is sorted and the count of elements that will make a valid pair with arr[i] will be (N – j) where N is the size of the given array.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the count of required pairs int count(int arr[], int n, int k) { // Sort the given array sort(arr, arr + n); // To store the required count int cnt = 0; int i = 0, j = 1; while (i < n && j < n) { // Update j such that it is always > i j = (j <= i) ? (i + 1) : j; // Find the first element arr[j] such that // (arr[j] - arr[i]) >= K // This is because after this element, all // the elements will have absolute differecne // with arr[i] >= k and the count of // valid pairs will be (n - j) while (j < n && (arr[j] - arr[i]) < k) j++; // Update the count of valid pairs cnt += (n - j); // Get to the next element to repeat the steps i++; } // Return the count return cnt; } // Driver code int main() { int arr[] = { 1, 2, 3, 4 }; int n = sizeof(arr) / sizeof(arr[0]); int k = 2; cout << count(arr, n, k); return 0; } |
Python3
# Python3 implementation of the approach # Function to return the count of required pairs def count(arr, n, k) : # Sort the given array arr.sort(); # To store the required count cnt = 0; i = 0; j = 1; while (i < n and j < n) : # Update j such that it is always > i if j <= i : j = i + 1 else : j = j # Find the first element arr[j] such that # (arr[j] - arr[i]) >= K # This is because after this element, all # the elements will have absolute differecne # with arr[i] >= k and the count of # valid pairs will be (n - j) while (j < n and (arr[j] - arr[i]) < k) : j += 1; # Update the count of valid pairs cnt += (n - j); # Get to the next element to repeat the steps i += 1; # Return the count return cnt; # Driver code if __name__ == "__main__" : arr = [ 1, 2, 3, 4 ]; n = len(arr); k = 2; print(count(arr, n, k)); # This code is contributed by AnkitRai01 |
3
Recommended Posts:
- Count maximum elements of an array whose absolute difference does not exceed K
- Minimum sum of absolute difference of pairs of two arrays
- Sum of absolute differences of all pairs in a given array
- Sort an array according to absolute difference with given value
- Maximum sum of absolute difference of an array
- Triplets in array with absolute difference less than k
- Maximum absolute difference in an array
- Sort an array according to absolute difference with given value using Functors
- k-th smallest absolute difference of two elements in an array
- Absolute Difference of even and odd indexed elements in an Array
- Python | Sort an array according to absolute difference
- Sum of minimum absolute difference of each array element
- Absolute Difference of all pairwise consecutive elements in an array
- Check if array can be divided into two sub-arrays such that their absolute difference is K
- Minimum absolute difference of adjacent elements in a circular array
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.
Improved By : AnkitRai01



