Count pairs in an array such that the absolute difference between them is ≥ K
Last Updated :
07 Jun, 2022
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 difference
// 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;
}
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 difference
// 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));
}
}
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 difference
# 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
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 difference
// 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.
JavaScript
<script>
// JavaScript implementation of the approach
// Function to return the count of required pairs
function count(arr, n, k) {
// Sort the given array
arr.sort();
// To store the required count
var cnt = 0;
var i = 0;
var j = 1;
while (i < n && 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 difference
// with arr[i] >= k and the count of
// valid pairs will be (n - j)
while (j < n && (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
var arr = [ 1, 2, 3, 4 ];
var n = arr.length;
var k = 2;
document.write(count(arr, n, k));
// This code is contributed by AnkThon
</script>
Time Complexity: O(n * log n)
Auxiliary Space: O(1)
Similar Reads
Count pairs such that difference between them and indices are different Given an array arr[] of size N, the task is to count all pair of indices (i, j) such that i < j and j - i != arr[j] - arr[i]. Examples: Input: arr[] = {4, 1, 3, 3}Output: 5Explanation:The pair (0, 1) is a valid pair since 1 - 0 != 1 - 4.The pair (0, 2) is a valid pair since 2 - 0 != 3 - 4, 2 != -
7 min read
Count pairs in an array whose absolute difference is divisible by K Given an array arr[] and a positive integer K. The task is to count the total number of pairs in the array whose absolute difference is divisible by K. Examples: Input: arr[] = {1, 2, 3, 4}, K = 2 Output: 2 Explanation: Total 2 pairs exists in the array with absolute difference divisible by 2. The p
14 min read
Count pairs in an array whose absolute difference is divisible by K | Using Map Given an array, arr[] of N elements and an integer K, the task is to find the number of pairs (i, j) such that the absolute value of (arr[i] - arr[j]) is a multiple of K. Examples: Input: N = 4, K = 2, arr[] = {1, 2, 3, 4}Output: 2Explanation: Total 2 pairs exists in the array with absolute differen
7 min read
Check if array can be divided into two sub-arrays such that their absolute difference is K Given an array arr[] and an integer K, the task is to find whether the array can be divided into two sub-arrays such that the absolute difference of the sum of the elements of both the sub-arrays is K. Examples: Input: arr[] = {2, 4, 5, 1}, K = 0 Output: Yes {2, 4} and {5, 1} are the two possible su
6 min read
Count of elements such that its sum/difference with X also exists in the Array Given an array arr[] and an integer X, the task is to count the elements of the array such that their exist a element arr[i] - X or arr[i] + X in the array.Examples: Input: arr[] = {3, 4, 2, 5}, X = 2 Output: 4 Explanation: In the above-given example, there are 4 such numbers - For Element 3: Possib
9 min read