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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `solution ` `{ ` ` ` `// Function to return the count of required pairs ` `static` `int` `count(` `int` `arr[], ` `int` `n, ` `int` `k) ` `{ ` ` ` ` ` `// Sort the given array ` ` ` `Arrays.sort(arr); ` ` ` ` ` `// 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 ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `arr[] = { ` `1` `, ` `2` `, ` `3` `, ` `4` `}; ` ` ` `int` `n = arr.length; ` ` ` `int` `k = ` `2` `; ` ` ` ` ` `System.out.println(count(arr, n, k)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the count of required pairs ` `static` `int` `count(` `int` `[]arr, ` `int` `n, ` `int` `k) ` `{ ` ` ` ` ` `// Sort the given array ` ` ` `Array.Sort(arr); ` ` ` ` ` `// 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 ` `static` `public` `void` `Main () ` `{ ` ` ` ` ` `int` `[]arr = { 1, 2, 3, 4 }; ` ` ` `int` `n = arr.Length; ` ` ` `int` `k = 2; ` ` ` ` ` `Console.Write(count(arr, n, k)); ` ` ` `} ` `} ` ` ` `// This code is contributed by jit_t. ` |

*chevron_right*

*filter_none*

**Output:**

3

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:

- Count the pairs in an array such that the difference between them and their indices is equal
- Minimize remaining array element by removing pairs and replacing them by their absolute difference
- Count pairs in an array whose absolute difference is divisible by K
- Count of all pairs in an Array with minimum absolute difference
- Count all disjoint pairs having absolute difference at least K from a given array
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Count of numbers whose difference with Fibonacci count upto them is atleast K
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Count of arrays in which all adjacent elements are such that one of them divide the another
- Check whether it is possible to join two points given on circle such that distance between them is k
- Generate two BSTs from the given array such that maximum height among them is minimum
- Minimum points to be selected such that removal of line segments passing through them empties given array
- Minimum value of maximum absolute difference of all adjacent pairs in an Array
- Make all array elements equal by repeated subtraction of absolute difference of pairs from their maximum
- Remove Minimum coins such that absolute difference between any two piles is less than K
- Longest subsequence such that absolute difference between every pair is atmost 1
- Maximum time such that absolute difference between hour and minute lies in given range
- Ways to form n/2 pairs such that difference of pairs is minimum
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Check if array can be divided into two sub-arrays such that their absolute difference is K

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.