📜  最多重复K且不重复元素的对数

📅  最后修改于: 2021-05-07 05:35:26             🧑  作者: 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 )初始化为零,并运行一会儿循环,直到索引计数器小于(长度– 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


输出:
2

性能分析:

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