📌  相关文章
📜  计算差异等于 K | 的所有不同对设置 2

📅  最后修改于: 2022-05-13 01:56:10.539000             🧑  作者: Mango

计算差异等于 K | 的所有不同对设置 2

给定一个整数数组arr[]和一个正整数 K,任务是计算所有差异等于K的不同对。

例子:

本文的Set 1中提到了朴素的方法和基于排序和二分查找的方法。

方法:通过在无序映射的帮助下使用散列,可以将这个问题的时间复杂度降低到平均情况下的线性复杂度,如下所示:

请按照以下步骤解决问题:

  • 初始化一个无序映射并将所有数组元素推送到映射中。
  • 如果K的给定值为0
    • 如果当前元素x的频率大于 1,则将count加 1。
    • 其他元素也可以尝试相同的方法。
  • 如果K的给定值不为 0:
    • 在地图中搜索x + K ,如果找到,则将计数加 1。
    • 否则尝试其他元素。
  • 返回计数

下面是上述方法的实现。

C++
// C++ code to implement the above approach.
 
#include 
using namespace std;
 
// Function to find total pairs
int TotalPairs(vector nums, int K)
{
    // Initializing a map
    unordered_map mp;
    int cnt = 0;
 
    for (int i = 0; i < nums.size(); i++) {
        mp[nums[i]]++;
    }
 
    // Difference equal to zero
    if (K == 0) {
        for (auto i : mp) {
 
            // Frequency of element is
            // greater than one then
            // distinct pair is possible
            if (i.second > 1)
                cnt++;
        }
    }
 
    // Difference is not equal to zero
    else {
        for (auto i : mp) {
 
            // Frequency of element + k
            // is not zero then distinct
            // pair is possible
            if (mp.find(i.first + K)
                != mp.end()) {
                cnt++;
            }
        }
    }
    return cnt;
}
 
// Driver Code
int main()
{
    vector arr = { 8, 12, 16, 4, 0, 20 };
    int K = 4;
 
    // Function call
    int ans = TotalPairs(arr, K);
    cout << ans;
    return 0;
}


Java
// Java code to implement the above approach.
import java.io.*;
import java.util.*;
 
class GFG {
    // Function to find total pairs
    public static int TotalPairs(int nums[], int K)
    {
        // Initializing a map
        Map mp
            = new HashMap();
        int cnt = 0;
 
        for (int i = 0; i < nums.length; i++) {
            if (mp.get(nums[i]) != null)
                mp.put(nums[i], mp.get(nums[i]) + 1);
            else
                mp.put(nums[i], 1);
        }
 
        // Difference equal to zero
        if (K == 0) {
            for (Map.Entry it :
                 mp.entrySet()) {
 
                // Frequency of element is
                // greater than one then
                // distinct pair is possible
                if (it.getValue() > 1)
                    cnt++;
            }
        }
 
        // Difference is not equal to zero
        else {
            for (Map.Entry it :
                 mp.entrySet()) {
 
                // Frequency of element + k
                // is not zero then distinct
                // pair is possible
                if (mp.get(it.getKey() + K) != null) {
                    cnt++;
                }
            }
        }
        return cnt;
    }
    public static void main(String[] args)
    {
        int arr[] = { 8, 12, 16, 4, 0, 20 };
        int K = 4;
 
        // Function call
        int ans = TotalPairs(arr, K);
        System.out.print(ans);
    }
}
 
// This code is contributed by Rohit Pradhan


Python3
# Python3 program for above approach
 
# function to find total pairs
def TotalPairs(nums, K):
   
    # Initializing a map or dictionary
    mp = dict()
    cnt = 0
    for i in range(len(nums)):
        if nums[i] in mp:
            mp[nums[i]] += 1
        else:
            mp[nums[i]] = 1
 
    # Difference equal to zero
    if K == 0:
        for i in mp:
            # Frequency of element is
            # greater than one then
            # distinct pair is possible
            if mp[i] > 1:
                cnt += 1
    # Difference is not equal to zero
    else:
        for i in mp:
            # Frequency of element + k
            # is not zero then distinct
            #pair is possible
            if i + K in mp:
                cnt += 1
 
    return cnt
 
# Driver Code
arr = [8, 12, 16, 4, 0, 20]
K = 4
 
# Function call
ans = TotalPairs(arr, K)
print(ans)
 
# This code is contributed by phasing17


输出
5

时间复杂度: O(N) [在平均情况下,因为无序映射的平均情况时间复杂度是 O(1)]
辅助空间: O(N)