📌  相关文章
📜  计算数组中的对,使得一个的频率至少是另一个的值

📅  最后修改于: 2021-10-27 08:29:54             🧑  作者: Mango

给定一个整数数组 A[]。任务是找到有序的正整数对 (X, Y) 的总数,使得 X 在 A[] 中至少出现 Y 次,而 Y 在 A 中至少出现 X 次。

例子

Input : A[] = { 1, 1, 2, 2, 3 }
Output : 4
Ordered pairs are -> { [1, 1], [1, 2], [2, 1], [2, 2] }

Input : A = { 3, 3, 2, 2, 2 }
Output : 3
Ordered pairs are -> { [3, 2], [2, 2], [2, 3] }

方法:

  1. 创建一个包含数组 A[] 元素计数的哈希表 m[]。
  2. 遍历唯一元素的哈希表。设 X 是哈希表中的当前键 Y 是它的频率。
  3. 检查每个元素 j = (1 to Y),如果 m[ j ] >= X 将 answer 增加 1。
  4. 返回数组 A 的总有序对 (X, Y) 的计数。

下面是上述方法的实现:

C++
// C++ program to find number
// of ordered pairs
  
#include 
using namespace std;
  
// Function to find count of Ordered pairs
int countOrderedPairs(int A[], int n)
{
    // Initialize pairs to 0
    int orderedPairs = 0;
  
    // Store frequencies 
    unordered_map m;
    for (int i = 0; i < n; ++i) 
        m[A[i]]++;
      
    // Count total Ordered_pairs
    for (auto entry : m) {
        int X = entry.first;
        int Y = entry.second;
  
        for (int j = 1; j <= Y; j++) {
            if (m[j] >= X)
                orderedPairs++;
        }
    }
  
    return orderedPairs;
}
  
// Driver Code
int main()
{
    int A[] = { 1, 1, 2, 2, 3 };
    int n = sizeof(A) / sizeof(A[0]);
    cout << countOrderedPairs(A, n);
    return 0;
}


Java
// Java program to find number
// of ordered pairs
import java.util.HashMap;
import java.util.Map;
  
class GFG
{
      
    // Function to find count of Ordered pairs
    public static int countOrderedPairs(int[] A, int n) 
    {
  
        // Initialize pairs to 0
        int orderedPairs = 0;
  
        // Store frequencies
        HashMap m = new HashMap<>();
        for (int i = 0; i < n; i++)
        {
            if (m.get(A[i]) == null)
                m.put(A[i], 1);
            else
            {
                int a = m.get(A[i]);
                m.put(A[i], ++a);
            }
        }
  
        // Count total Ordered_pairs
        for (int entry : m.keySet())
        {
              
            int X = entry;
            int Y = m.get(entry);
  
            for (int j = 1; j <= Y; j++)
            {
                if (m.get(j) >= X)
                    orderedPairs++;
            }
        }
  
        return orderedPairs;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int[] A = {1, 1, 2, 2, 3};
        int n = A.length;
        System.out.print(countOrderedPairs(A, n));
    }
}
  
// This code is contibuted by
// sanjeev2552


Python3
# Python3 program to find the 
# number of ordered pairs 
from collections import defaultdict
  
# Function to find count of Ordered pairs 
def countOrderedPairs(A, n): 
  
    # Initialize pairs to 0 
    orderedPairs = 0
  
    # Store frequencies 
    m = defaultdict(lambda:0)
    for i in range(0, n): 
        m[A[i]] += 1
      
    # Count total Ordered_pairs 
    for X,Y in m.items(): 
          
        for j in range(1, Y + 1): 
            if m[j] >= X: 
                orderedPairs += 1
          
    return orderedPairs 
  
# Driver Code 
if __name__ == "__main__":
  
    A = [1, 1, 2, 2, 3] 
    n = len(A) 
    print(countOrderedPairs(A, n)) 
      
# This code is contributed by Rituraj Jain


C#
// C# program to illustrate how 
// to create a dictionary 
using System; 
using System.Collections.Generic; 
  
class GFG
{
      
    // Function to find count of Ordered pairs
    public static int countOrderedPairs(int[] A,            
                                        int n) 
    {
  
        // Initialize pairs to 0
        int orderedPairs = 0;
  
        // Store frequencies
        Dictionary m = new Dictionary();
        for (int i = 0; i < n; i++)
        {
            if (!m.ContainsKey(A[i]))
                m.Add(A[i], 1);
            else
            {
                m[A[i]]++;
            }
        }
  
        // Count total Ordered_pairs
        foreach(KeyValuePair entry in m)
        {
              
            int X = entry.Key;
            int Y = entry.Value;
  
            for (int j = 1; j <= Y; j++)
            {
                if (m[j] >= X)
                    orderedPairs++;
            }
        }
        return orderedPairs;
    }
  
    // Driver Code
    public static void Main()
    {
        int[] A = {1, 1, 2, 2, 3};
        int n = A.Length;
        Console.Write(countOrderedPairs(A, n));
    }
}
  
// This code is contibuted by
// mohit kumar


输出:
4