📌  相关文章
📜  对数组中的对进行计数,以使它们之间的绝对差≥K

📅  最后修改于: 2021-05-04 07:48:43             🧑  作者: Mango

给定一个数组arr []和一个整数K ,任务是从数组中找到对数(arr [i],arr [j]) ,使得| arr [i] – arr [j] | ≥K注意(arr [i],arr [j])arr [j],arr [i]仅被计数一次。
例子:

方法:对给定的数组进行排序。现在,对于每个元素arr [i] ,找到右边arr [j]的第一个元素,使得(arr [j] – arr [i])≥K 。这是因为在此元素之后,每个元素在数组排序时都将使用arr [i]满足相同条件,并且将与arr [i]组成有效对的元素数将为(N – j) ,其中N为给定数组的大小。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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;
}


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


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


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.


Javascript


输出:
3