Number of pairs in an array having sum equal to product | Function to return the count of the required pairs ; Find the count of 0 s and 2 s in the array ; Find the count of required pairs ; Return the count ; Driver code
function sumEqualProduct ( a , n ) { var zero = 0 , two = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { zero ++ ; } if ( a [ i ] == 2 ) { two ++ ; } } var cnt = ( zero * ( zero - 1 ) ) / 2 + ( two * ( two - 1 ) ) / 2 ; return cnt ; } var a = [ 2 , 2 , 3 , 4 , 2 , 6 ] ; var n = a . length ; document . write ( sumEqualProduct ( a , n ) ) ;