Count of pairs in an array such that the highest power of 2 that divides their product is 1 | Java implementation of the approach ; 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
class GFG { static int cntPairs ( int a [ ] , int n ) { int odd = 0 , even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) odd ++ ; else if ( ( a [ i ] / 2 ) % 2 == 1 ) even ++ ; } int ans = odd * even + ( odd * ( odd - 1 ) ) / 2 ; return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 4 , 2 , 7 , 11 , 14 , 15 , 18 } ; int n = a . length ; System . out . println ( cntPairs ( a , n ) ) ; } }