Count pairs in an array such that frequency of one is at least value of other | C # program to illustrate how to create a dictionary ; Function to find count of Ordered pairs ; Initialize pairs to 0 ; Store frequencies ; Count total Ordered_pairs ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public static int countOrderedPairs ( int [ ] A , int n ) { int orderedPairs = 0 ; Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! m . ContainsKey ( A [ i ] ) ) m . Add ( A [ i ] , 1 ) ; else { m [ A [ i ] ] ++ ; } } foreach ( KeyValuePair < int , int > 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 ; } public static void Main ( ) { int [ ] A = { 1 , 1 , 2 , 2 , 3 } ; int n = A . Length ; Console . Write ( countOrderedPairs ( A , n ) ) ; } }