Maximum count of pairwise co | Java program for the above approach ; Function to find the gcd of two numbers ; Function to of pairwise co - prime and common divisors of two numbers ; Initialize answer with 1 , to include 1 in the count ; Count of primes of gcd ( N , M ) ; Finding prime factors of gcd ; Increment count if it is divisible by i ; Return the total count ; Function Call for each pair to calculate the count of pairwise co - prime divisors ; Driver Code ; Given array of pairs ; Function Call
class GFG { static int gcd ( int x , int y ) { if ( x % y == 0 ) return y ; else return gcd ( y , x % y ) ; } static int countPairwiseCoprime ( int N , int M ) { int answer = 1 ; int g = gcd ( N , M ) ; int temp = g ; for ( int i = 2 ; i * i <= g ; i ++ ) { if ( temp % i == 0 ) { answer ++ ; while ( temp % i == 0 ) temp /= i ; } } if ( temp != 1 ) answer ++ ; return answer ; } static void countCoprimePair ( int arr [ ] [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( countPairwiseCoprime ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 12 , 18 } , { 420 , 660 } } ; int N = arr . length ; countCoprimePair ( arr , N ) ; } }