Number of pairs such that their HCF and LCM is equal | C # program to count number of pairs such that their hcf and lcm are equal ; Function to count number of pairs such that their hcf and lcm are equal ; Store frequencies of array elements ; Count of pairs ( arr [ i ] , arr [ j ] ) where arr [ i ] = arr [ j ] ; Count of pairs ( arr [ i ] , arr [ j ] ) where arr [ i ] = arr [ j ] , ; Driver function
using System ; using System . Collections . Generic ; class GFG { static int countPairs ( int [ ] a , int n ) { Dictionary < int , int > frequency = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( frequency . ContainsKey ( a [ i ] ) ) { var val = frequency [ a [ i ] ] ; frequency . Remove ( a [ i ] ) ; frequency . Add ( a [ i ] , val + 1 ) ; } else { frequency . Add ( a [ i ] , 1 ) ; } } int count = 0 ; foreach ( KeyValuePair < int , int > entry in frequency ) { int f = entry . Value ; count += f * ( f - 1 ) / 2 ; } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 } ; int n = arr . Length ; Console . Write ( countPairs ( arr , n ) ) ; } }