Count pairs from an array having equal sum and quotient | Function to find number of pairs with equal sum and quotient from a given array ; Store the count of pairs ; Stores frequencies ; Traverse the array ; If y is neither 1 or 0 ; Evaluate x ; Increment count by frequency of x ; Update map ; Prlet the final count ; Driver Code ; Function Call
function countPairs ( a , n ) { let count = 0 ; let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { let y = a [ i ] ; if ( y != 0 && y != 1 ) { let x = ( ( y * 1.0 ) / ( 1 - y ) ) * y ; if ( mp . has ( x ) ) count += mp . get ( x ) ; } if ( mp . has ( y ) ) { mp . set ( y , mp . get ( y ) + 1 ) ; } else { mp . set ( y , 1 ) ; } } return count ; } let arr = [ - 4 , - 3 , 0 , 2 , 1 ] ; let N = arr . length ; document . write ( countPairs ( arr , N ) ) ;