Count number of pairs ( i , j ) from an array such that arr [ i ] * j = arr [ j ] * i | C # program for the above approach ; Function to count pairs from an array satisfying given conditions ; Stores the total count of pairs ; Stores count of a [ i ] / i ; Traverse the array ; Updating count ; Update frequency in the Map ; Print count of pairs ; Driver Code ; Given array ; Size of the array ; Function call to count pairs satisfying given conditions
using System ; using System . Collections . Generic ; class GFG { static void countPairs ( int [ ] arr , int N ) { int count = 0 ; Dictionary < double , int > mp = new Dictionary < double , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { double val = 1.0 * arr [ i ] ; double idx = 1.0 * ( i + 1 ) ; if ( mp . ContainsKey ( val / idx ) ) count += mp [ val / idx ] ; if ( mp . ContainsKey ( val / idx ) ) mp [ val / idx ] ++ ; else mp [ val / idx ] = 1 ; } Console . WriteLine ( count ) ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 5 , 6 , 5 } ; int N = arr . Length ; countPairs ( arr , N ) ; } }