Count pairs in an array such that frequency of one is at least value of other | C ++ program to find number of ordered pairs ; Function to find count of Ordered pairs ; Initialize pairs to 0 ; Store frequencies ; Count total Ordered_pairs ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOrderedPairs ( int A [ ] , int n ) { int orderedPairs = 0 ; unordered_map < int , int > m ; for ( int i = 0 ; i < n ; ++ i ) m [ A [ i ] ] ++ ; 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 ; } int main ( ) { int A [ ] = { 1 , 1 , 2 , 2 , 3 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << countOrderedPairs ( A , n ) ; return 0 ; }