Count number of pairs ( i , j ) from an array such that arr [ i ] * j = arr [ j ] * i | 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 ; Given array ; Size of the array ; Function call to count pairs satisfying given conditions
function countPairs ( arr , N ) { var count = 0 ; var mp = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { var val = 1.0 * arr [ i ] ; var idx = 1.0 * ( i + 1 ) ; count += mp . has ( val / idx ) ? mp . get ( val / idx ) : 0 if ( mp . has ( val / idx ) ) mp . set ( val / idx , mp . get ( val / idx ) + 1 ) else mp . set ( val / idx , 1 ) } document . write ( count ) ; } var arr = [ 1 , 3 , 5 , 6 , 5 ] ; var N = arr . length ; countPairs ( arr , N ) ;