Count of pairs in an array such that the highest power of 2 that divides their product is 1 | Function to return the count of valid pairs ; To store the count of odd numbers and the count of even numbers such that 2 is the only even factor of that number ; If current number is odd ; If current number is even and 2 is the only even factor of it ; Calculate total number of valid pairs ; Driver code
function cntPairs ( a , n ) { var odd = 0 , even = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) odd ++ ; else if ( ( a [ i ] / 2 ) % 2 == 1 ) even ++ ; } var ans = odd * even + ( odd * ( odd - 1 ) ) / 2 ; return ans ; } var a = [ 4 , 2 , 7 , 11 , 14 , 15 , 18 ] ; var n = a . length ; document . write ( cntPairs ( a , n ) ) ;