# Count pairs in an array such that frequency of one is at least value of other

Given an array A[] of integers. The task is to find the total number of ordered pairs of positive integers (X, Y) such that X occurs in A[] at least Y times and Y occurs in A at least X times.

**Examples**:

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] }

**Approach:**

- Create a hash table m[] of count of elements of array A[].
- Traverse the hash table of unique elements. Let X be current key in the hash table Y be its frequency.
- Check for each element j = (1 to Y) such that, if m[ j ] >= X increment answer by 1.
- Return the count of total ordered pairs (X, Y) of array A.

Below is the implementation of above approach:

## C++

`// C++ program to find number ` `// of ordered pairs ` ` ` `#include <bits/stdc++.h> ` `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<` `int` `, ` `int` `> 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; ` `} ` |

*chevron_right*

*filter_none*

## 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<Integer, Integer> 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

4

## Recommended Posts:

- Count number of occurrences (or frequency) in a sorted array
- Cumulative frequency of count of each element in an unsorted array
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j])
- Count of pairs (x, y) in an array such that x < y
- Count pairs in an array that hold i+j= arr[i]+arr[j]
- Count pairs in array whose sum is divisible by K
- Count divisible pairs in an array
- Count pairs in an array that hold i*arr[i] > j*arr[j]
- Count pairs in array whose sum is divisible by 4
- Count pairs with average present in the same array
- Count pairs whose products exist in array
- Count pairs in an array which have at least one digit common
- Count pairs in an array such that the absolute difference between them is ≥ K
- Count of pairs in an array whose sum is a perfect square
- Count number of pairs in array having sum divisible by K | SET 2

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.