📜  差异至多 K 且没有元素重复的对数

📅  最后修改于: 2021-09-07 05:10:11             🧑  作者: Mango

给定一个数组arr[]和一个数字K ,任务是计算差值小于或等于 K 的对的数量,这样一个元素只能被认为是一对。

例子:

方法:
思路是对数组进行排序,找出相邻元素的差值,如果差值最多为K,则考虑成对并增加计数,然后根据条件,任何元素只能在一对中,然后如果找到一对,则将计数器增加 2,这样任何元素都只出现在一对中。

例如:

Given Array - {1, 4, 3, 7, 5}, K = 2 
After Sorting Array will be - {1, 3, 4, 5, 7}
Step 1 - i = 0, count = 0
Consider the pair of elements for i and i + 1
Pair - (1, 3), Difference = 3 - 1 = 2
As the Difference is less than equal to 2
count = 1 and i = 2

Step 2 - i = 2, count = 1
Consider the pair of elements for i and i + 1
Pair - (4, 5), Difference = 5 - 4 = 1
As the Difference is less than equal to 2
count = 2 and i = 4
As i is greater than length-2,
there will be no more possible pairs.

算法:

  • 使用任何排序算法对数组进行排序,以便连续元素在一起。
  • 将索引计数器(例如i )初始化为零并运行 while 循环,直到索引计数器小于(长度 – 1)
    1. 检查索引ii + 1处元素的差异。
    2. 如果差值小于或等于 K,则将索引增加 2,并将计数器增加 1,以考虑立即增加元素。
    3. 否则,将索引增加 1 以考虑由下一个元素形成的对。

下面是上述方法的实现:

C++
// C++ implementation to count the
// number of pairs whose difference
// is atmost K in an array
#include 
#include
 
using namespace std;
 
    // Function to count the
    // number of pairs whose difference
    // is atmost K in an array
    int countPairs(int arr[], int k, int n)
    {
         
        // Variable to store the count of pairs
        // whose difference is atmost K
        int pair = 0;
        int index = 0;
         
        //int n = sizeof(arr)/sizeof(arr[0]);
         
        // Sorting the Array
        sort(arr,arr + n) ;
                 
        // Loop to consider the consecutive
        // pairs of the array
        while(index < n -1)
        {
             
            // if Pair found increment
            // the index by 2
            if (arr[index + 1] - arr[index] <= k){
                pair += 1 ;
                index += 2 ;
            }
            else{
                index += 1;
            }
        }
        return pair ;
     
    }
     
// Driver Code
int main() {
    int arr[] = {1, 4, 3, 7, 5} ;
    int k = 2;
     
     int n = sizeof(arr)/sizeof(arr[0]);
    // Function Call
    int count = countPairs(arr, k,n) ;
    cout << count << endl;;
}
 
// This code is contributed by AnkitRai01


Java
// Java implementation to count the
// number of pairs whose difference
// is atmost K in an array
import java.util.*;
 
class GFG
{
 
    // Function to count the
    // number of pairs whose difference
    // is atmost K in an array
    static int countPairs(int arr[], int k)
    {
         
        // Sorting the Array
        Arrays.sort(arr) ;
         
        // Variable to store the count of pairs
        // whose difference is atmost K
        int pair = 0;
        int index = 0;
         
        // Loop to consider the consecutive
        // pairs of the array
        while(index < arr.length -1)
        {
             
            // if Pair found increment
            // the index by 2
            if (arr[index + 1] - arr[index] <= k){
                pair += 1 ;
                index += 2 ;
            }
            else{
                index += 1;
            }
        }
        return pair ;
     
    }
     
    // Driver Code
    public static void main (String[] args) {
        int arr[] = {1, 4, 3, 7, 5} ;
        int k = 2;
         
        // Function Call
        int count = countPairs(arr, k) ;
        System.out.println(count);
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 implementation to count the
# number of pairs whose difference
# is atmost K in an array
 
 
# Function to count the
# number of pairs whose difference
# is atmost K in an array
def countPairs(arr, k):
     
    # Sorting the Array
    arr.sort()
     
    # Variable to store the count of pairs
    # whose difference is atmost K
    pair = 0
    index = 0
     
    # Loop to consider the consecutive
    # pairs of the array
    while(index < len(arr)-1):
         
        # if Pair found increment
        # the index by 2
        if arr[index + 1] - arr[index] <= k:
            pair += 1
            index += 2
        else:
            index += 1
             
    return pair
 
# Driver Code
if __name__ == "__main__":
    arr = [1, 4, 3, 7, 5]
    k = 2
    # Function Call
    count = countPairs(arr, k)
    print(count)


C#
// C# implementation to count the
// number of pairs whose difference
// is atmost K in an array
using System;
 
class GFG
{
 
    // Function to count the
    // number of pairs whose difference
    // is atmost K in an array
    static int countPairs(int []arr, int k)
    {
         
        // Sorting the Array
        Array.Sort(arr) ;
         
        // Variable to store the count of pairs
        // whose difference is atmost K
        int pair = 0;
        int index = 0;
         
        // Loop to consider the consecutive
        // pairs of the array
        while(index < arr.Length - 1)
        {
             
            // if Pair found increment
            // the index by 2
            if (arr[index + 1] - arr[index] <= k)
            {
                pair += 1 ;
                index += 2 ;
            }
            else
            {
                index += 1;
            }
        }
        return pair ;
    }
     
    // Driver Code
    public static void Main ()
    {
        int []arr = {1, 4, 3, 7, 5} ;
        int k = 2;
         
        // Function Call
        int count = countPairs(arr, k) ;
        Console.WriteLine(count);
    }
}
 
// This code is contributed by AnkitRai01


Javascript


输出:
2

性能分析:

  • 时间复杂度:在上面给出的方法中,对需要 O(N logN) 的数组进行排序,并且还有一次迭代来计算 O(N) 的对数。
    因此,该方法的整体复杂度为O(N logN + N)
  • 空间复杂度:在上面给出的方法中,没有额外的空间。因此,该方法的整体空间复杂度将为O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live