Saturday, September 21, 2024
Google search engine
HomeData Modelling & AICount pairs in an array such that the absolute difference between them...

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:
All valid pairs are (1, 3), (1, 4) and (2, 4)
Input: arr[] = {7, 4, 12, 56, 123}, K = 50 
Output:
 

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>


Output: 

3

 

Time Complexity: O(n * log n)

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments