codeparrot/xlcost-text-to-code huggingface dataset:
Minimize increments or decrements by 2 to convert given value to a perfect square | Java program to implement the above approach ; Function to find the minimum number of operations required to make N a perfect square ; Stores count of operations by performing decrements ; Stores value of N ; Decrement the value of temp ; Stores square root of temp ; If temp is a perfect square ; Update temp ; Store count of operations by performing increments ; Increment the value of N ; Stores sqrt of N ; If N is a perfect square ; Update temp ; Return the minimum count ; Driver code
class GFG { static int MinimumOperationReq ( int N ) { int cntDecr = 0 ; int temp = N ; while ( temp > 0 ) { int X = ( int ) Math . sqrt ( temp ) ; if ( X * X == temp ) { break ; } temp = temp - 2 ; cntDecr += 1 ; } int cntIncr = 0 ; while ( true ) { int X = ( int ) Math . sqrt ( N ) ; if ( X * X == N ) { break ; } N = N + 2 ; cntIncr += 1 ; } return Math . min ( cntIncr , cntDecr ) ; } public static void main ( String args [ ] ) { int N = 15 ; System . out . print ( MinimumOperationReq ( N ) ) ; } }
First term from given Nth term of the equation F ( N ) = ( 2 * F ( N | Java program to implement the above approach ; Function to find the value of power ( X , N ) % M ; Stores the value of ( X ^ N ) % M ; Calculate the value of power ( x , N ) % M ; If N is odd ; Update res ; Update x ; Update N ; Function to find modulo multiplicative inverse of X under modulo M ; Function to find the value of F ( 1 ) ; Stores power ( 2 , N - 1 ) ; Stores modulo multiplicative inverse of P_2 under modulo M ; Stores the value of F ( 1 ) ; Update res ; Driver code
import java . util . * ; class GFG { static final int M = 1000000007 ; static long power ( long x , long N ) { long res = 1 ; while ( N > 0 ) { if ( N % 2 == 1 ) { res = ( res * x ) % M ; } x = ( x * x ) % M ; N = N >> 1 ; } return res ; } static long moduloInverse ( long X ) { return power ( X , M - 2 ) ; } static long F_1 ( long N , long F_N ) { long P_2 = power ( 2 , N - 1 ) ; long modInv = moduloInverse ( P_2 ) ; long res ; res = ( ( modInv % M ) * ( F_N % M ) ) % M ; return res ; } public static void main ( String [ ] args ) { long N = 3 ; long F_N = 6 ; System . out . print ( F_1 ( N , F_N ) ) ; } }
Queries to replace every array element by its XOR with a given value with updates | Java program of the above approach ; Function to insert an element into the array ; Function to update every array element a [ i ] by a [ i ] ^ x ; Function to compute the final results after the operations ; Driver Code ; Queries ; Function Call
import java . util . * ; class GFG { static Vector < Integer > arr = new Vector < Integer > ( ) ; static int effect ; static void add ( int x ) { arr . add ( x ) ; } static void update ( int x ) { effect = effect ^ x ; } static void computeResults ( ) { for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { arr . set ( i , arr . get ( i ) ^ effect ) ; System . out . print ( arr . get ( i ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { arr = new Vector < Integer > ( ) ; arr . add ( 0 ) ; effect = 0 ; add ( 5 ) ; update ( 2 ) ; computeResults ( ) ; } }
Check if a subarray of size K exists whose elements form a number divisible by 3 | Java implementation of the above approach ; Function to find the K size subarray ; Check if the first K elements forms a number which is divisible by 3 ; Using Sliding window technique ; Calculate sum of next K size subarray ; Check if sum is divisible by 3 ; Update the indices of the subarray ; If no such subarray is found ; Print the subarray ; Driver code ; Given array and K ; Function call
import java . util . * ; import java . awt . Point ; class GFG { public static void findSubArray ( Vector < Integer > arr , int k ) { Point ans = new Point ( 0 , 0 ) ; int i , sum = 0 ; for ( i = 0 ; i < k ; i ++ ) { sum += arr . get ( i ) ; } int found = 0 ; if ( sum % 3 == 0 ) { ans = new Point ( 0 , i - 1 ) ; found = 1 ; } for ( int j = i ; j < arr . size ( ) ; j ++ ) { if ( found == 1 ) break ; sum = sum + arr . get ( j ) - arr . get ( j - k ) ; if ( sum % 3 == 0 ) { ans = new Point ( j - k + 1 , j ) ; found = 1 ; } } if ( found == 0 ) ans = new Point ( - 1 , 0 ) ; if ( ans . x == - 1 ) { System . out . print ( - 1 ) ; } else { for ( i = ans . x ; i <= ans . y ; i ++ ) { System . out . print ( arr . get ( i ) + " ▁ " ) ; } } } public static void main ( String [ ] args ) { Vector < Integer > arr = new Vector < Integer > ( ) ; arr . add ( 84 ) ; arr . add ( 23 ) ; arr . add ( 45 ) ; arr . add ( 12 ) ; arr . add ( 56 ) ; arr . add ( 82 ) ; int K = 3 ; findSubArray ( arr , K ) ; } }
Count permutations of given array that generates the same Binary Search Tree ( BST ) | Java program to implement the above approach ; Function to precompute the factorial of 1 to N ; Function to get the value of nCr ; nCr = fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; Function to count the number of ways to rearrange the array to obtain same BST ; Store the size of the array ; Base case ; Store the elements of the left subtree of BST ; Store the elements of the right subtree of BST ; Store the root node ; Push all the elements of the left subtree ; Push all the elements of the right subtree ; Store the size of leftSubTree ; Store the size of rightSubTree ; Recurrence relation ; Driver Code ; Store the size of arr ; Store the factorial up to N ; Precompute the factorial up to N
import java . util . * ; class GFG { static void calculateFact ( int fact [ ] , int N ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { fact [ i ] = fact [ i - 1 ] * i ; } } static int nCr ( int fact [ ] , int N , int R ) { if ( R > N ) return 0 ; int res = fact [ N ] / fact [ R ] ; res /= fact [ N - R ] ; return res ; } static int countWays ( Vector < Integer > arr , int fact [ ] ) { int N = arr . size ( ) ; if ( N <= 2 ) { return 1 ; } Vector < Integer > leftSubTree = new Vector < Integer > ( ) ; Vector < Integer > rightSubTree = new Vector < Integer > ( ) ; int root = arr . get ( 0 ) ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr . get ( i ) < root ) { leftSubTree . add ( arr . get ( i ) ) ; } else { rightSubTree . add ( arr . get ( i ) ) ; } } int N1 = leftSubTree . size ( ) ; int N2 = rightSubTree . size ( ) ; int countLeft = countWays ( leftSubTree , fact ) ; int countRight = countWays ( rightSubTree , fact ) ; return nCr ( fact , N - 1 , N1 ) * countLeft * countRight ; } public static void main ( String [ ] args ) { int [ ] a = { 3 , 4 , 5 , 1 , 2 } ; Vector < Integer > arr = new Vector < Integer > ( ) ; for ( int i : a ) arr . add ( i ) ; int N = a . length ; int [ ] fact = new int [ N ] ; calculateFact ( fact , N ) ; System . out . print ( countWays ( arr , fact ) ) ; } }
Sum of all ordered pair | Java program to implement the above approach ; Function to calculate the sum of all pair - products ; Stores sum of array ; Update sum of the array ; Driver code
import java . util . * ; class GFG { static int sumOfProd ( int arr [ ] , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } return sum * sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 1 , 5 , 4 } ; int N = arr . length ; System . out . print ( sumOfProd ( arr , N ) ) ; } }
Check if a given number is one less than twice its reverse | Java program of the above approach ; Iterative function to reverse digits of num ; Loop to extract all digits of the number ; Function to check if N satisfies given equation ; Driver Code
import java . util . * ; class GFG { static int rev ( int num ) { int rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = num / 10 ; } return rev_num ; } static boolean check ( int n ) { return 2 * rev ( n ) == n + 1 ; } public static void main ( String [ ] args ) { int n = 73 ; if ( check ( n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Check if a given number is one less than twice its reverse | Java program to implement the above approach ; Function to check y is a power of x ; logarithm function to calculate value ; Compare to the result1 or result2 both are equal ; Function to check if N satisfies the equation 2 * reverse ( n ) = n + 1 ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static boolean isPower ( int x , int y ) { double res1 = Math . log ( y ) / Math . log ( x ) ; double res2 = Math . log ( y ) / Math . log ( x ) ; return ( res1 == res2 ) ; } static boolean check ( int n ) { int x = ( n + 7 ) / 8 ; if ( ( n + 7 ) % 8 == 0 && isPower ( 10 , x ) ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 73 ; if ( check ( n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Nth term of a recurrence relation generated by two given arrays | Java program for the above approach ; Function to calculate Nth term of general recurrence relations ; Stores the generated sequence ; Current term is sum of previous k terms ; Print the nth term ; Driver Code ; Given Array F [ ] and C [ ] ; Given N and K ; Function call
import java . util . * ; import java . lang . * ; class GFG { static double mod = 1e9 + 7 ; static void NthTerm ( int F [ ] , int C [ ] , int K , int n ) { int ans [ ] = new int [ n + 1 ] ; for ( int i = 0 ; i < K ; i ++ ) ans [ i ] = F [ i ] ; for ( int i = K ; i <= n ; i ++ ) { for ( int j = i - K ; j < i ; j ++ ) { ans [ i ] += ans [ j ] ; ans [ i ] %= mod ; } } System . out . println ( ans [ n ] ) ; } public static void main ( String [ ] args ) { int F [ ] = { 0 , 1 } ; int C [ ] = { 1 , 1 } ; int K = 2 ; int N = 10 ; NthTerm ( F , C , K , N ) ; } }
Mode of frequencies of given array elements | Java program of the above approach ; Function to find the mode of the frequency of the array ; Stores the frequencies of array elements ; Traverse through array elements and count frequencies ; Stores the frequencies 's of  frequencies of array elements ; Stores the minimum value ; Find the Mode in 2 nd map ; Search for this Mode ; When mode is find then return to main function . ; If mode is not found ; Driver Code
import java . util . * ; class GFG { static int countFreq ( int arr [ ] , int n ) { HashMap < Integer , Integer > mp1 = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( mp1 . containsKey ( arr [ i ] ) ) { mp1 . put ( arr [ i ] , mp1 . get ( arr [ i ] ) + 1 ) ; } else { mp1 . put ( arr [ i ] , 1 ) ; } } HashMap < Integer , Integer > mp2 = new HashMap < Integer , Integer > ( ) ; for ( Map . Entry < Integer , Integer > it : mp1 . entrySet ( ) ) { if ( mp2 . containsKey ( it . getValue ( ) ) ) { mp2 . put ( it . getValue ( ) , mp2 . get ( it . getValue ( ) ) + 1 ) ; } else { mp2 . put ( it . getValue ( ) , 1 ) ; } } int M = Integer . MIN_VALUE ; for ( Map . Entry < Integer , Integer > it : mp2 . entrySet ( ) ) { M = Math . max ( M , it . getValue ( ) ) ; } for ( Map . Entry < Integer , Integer > it : mp2 . entrySet ( ) ) { if ( M == it . getValue ( ) ) { return it . getKey ( ) ; } } return 0 ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 10 , 3 , 10 , 8 , 3 , 6 , 4 } ; int n = arr . length ; System . out . print ( countFreq ( arr , n ) ) ; } }
Maximize count of nodes disconnected from all other nodes in a Graph | Java implementation of the above approach ; Function which returns the maximum number of isolated nodes ; Used nodes ; Remaining edges ; Count nodes used ; If given edges are non - zero ; Driver Code ; Given N and E ; Function call
import java . util . * ; class GFG { static int maxDisconnected ( int N , int E ) { int curr = 1 ; int rem = E ; while ( rem > 0 ) { rem = rem - Math . min ( curr , rem ) ; curr ++ ; } if ( curr > 1 ) { return N - curr ; } else { return N ; } } public static void main ( String [ ] args ) { int N = 5 , E = 1 ; System . out . print ( maxDisconnected ( N , E ) ) ; } }
Check if number is palindrome or not in base B | Java program to implement the above approach ; Function to check if N in base B is palindrome or not ; Stores the reverse of N ; Stores the value of N ; Extract all the digits of N ; Generate its reverse ; Driver code
class GFG { static boolean checkPalindromeB ( int N , int B ) { int rev = 0 ; int N1 = N ; while ( N1 > 0 ) { rev = rev * B + N1 % B ; N1 = N1 / B ; } return N == rev ; } public static void main ( String [ ] args ) { int N = 5 , B = 2 ; if ( checkPalindromeB ( N , B ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
Product of all numbers up to N that are co | Java program for the above approach ; Function to return gcd of a and b ; Base Case ; Recursive GCD ; Function to find the product of all the numbers till N that are relatively prime to N ; Stores the resultant product ; Iterate over [ 2 , N ] ; If gcd is 1 , then find the product with result ; Return the final product ; Driver Code
import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int findProduct ( int N ) { int result = 1 ; for ( int i = 2 ; i < N ; i ++ ) { if ( gcd ( i , N ) == 1 ) { result *= i ; } } return result ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . print ( findProduct ( N ) ) ; } }
Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum | Java implementation of the above approach ; Function to count maximum number of array elements equal ; Driver Code ; Function call
import java . io . * ; class GFG { static int countEqual ( int n ) { return ( n + 1 ) / 2 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = arr . length ; System . out . println ( countEqual ( n ) ) ; } }
Minimize count of unequal elements at corresponding indices between given arrays | Java program for the above approach ; Function that count of the mismatched pairs in bot the array ; Create a parent array and initialize it ; Preprocessing of the given pairs of indices ; 1 - based indexing ; If both indices doesn 't belong  to same component ; Insert the indices in same component ; HashMap to get the indices of array A ; If current element is not present in array B then count this as mismatched ; Get the index of the element in array B ; Check if both indices belong to same connected component if not increment the count ; Return answer ; Function that gives the connected component of each index ; Function that creates the connected components ; Find parent of a and b recursively ; Update the parent of a ; Driver Code ; Given arrays A [ ] , B [ ] ; List of indices ; Function Call
import java . util . * ; class GFG { public static int countPairs ( int A [ ] , int B [ ] , int N , int M , int [ ] [ ] List ) { int count = 0 ; int par [ ] = new int [ N + 1 ] ; for ( int i = 0 ; i <= N ; i ++ ) par [ i ] = i ; for ( int i = 0 ; i < M ; i ++ ) { int index1 = find ( par , List [ i ] [ 0 ] - 1 ) ; int index2 = find ( par , List [ i ] [ 1 ] - 1 ) ; if ( index1 != index2 ) { union ( par , index1 , index2 ) ; } } HashMap < Integer , Integer > map = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { map . put ( A [ i ] , i ) ; } for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] != B [ i ] ) { if ( ! map . containsKey ( B [ i ] ) ) { count ++ ; continue ; } int j = map . get ( B [ i ] ) ; if ( find ( par , i ) != find ( par , j ) ) count ++ ; } } return count ; } public static int find ( int par [ ] , int x ) { if ( par [ x ] == x ) return x ; else return par [ x ] = find ( par , par [ x ] ) ; } public static void union ( int par [ ] , int a , int b ) { a = find ( par , a ) ; b = find ( par , b ) ; if ( a == b ) return ; par [ a ] = b ; } public static void main ( String [ ] args ) { int N = 5 ; int M = 4 ; int A [ ] = { 1 , 5 , 9 , 2 , 3 } ; int B [ ] = { 2 , 4 , 5 , 1 , 3 } ; int List [ ] [ ] = { { 1 , 4 } , { 2 , 3 } , { 3 , 5 } , { 2 , 5 } } ; System . out . println ( countPairs ( A , B , N , M , List ) ) ; } }
Maximum value of expression ( arr [ i ] + arr [ j ] * arr [ k ] ) formed from a valid Triplet | Java program for the above approach ; Function that generate all valid triplets and calculate the value of the valid triplets ; Generate all triplets ; Check whether the triplet is valid or not ; Update the value ; Print the maximum value ; Driver Code ; Given array arr [ ] ; Function Call
import java . util . Scanner ; class GFG { static void max_valid_triplet ( int A [ ] , int n ) { int ans = - 1 ; for ( int i = 0 ; i < n - 2 ; i ++ ) { for ( int j = i + 1 ; j < n - 1 ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { if ( A [ i ] < A [ j ] && A [ j ] < A [ k ] ) { int value = A [ i ] + A [ j ] * A [ k ] ; if ( value > ans ) { ans = value ; } } } } } System . out . println ( ans ) ; } public static void main ( String args [ ] ) { int [ ] arr = new int [ ] { 7 , 9 , 3 , 8 , 11 , 10 } ; int n = arr . length ; max_valid_triplet ( arr , n ) ; } }
Product of proper divisors of a number for Q queries | Java implementation of the above approach ; Function to precompute the product of proper divisors of a number at it 's corresponding index ; Returning the pre - computed values ; Driver code
import java . util . * ; class GFG { static final int mod = 1000000007 ; static long [ ] ans = new long [ 100002 ] ; static void preCompute ( ) { for ( int i = 2 ; i <= 100000 / 2 ; i ++ ) { for ( int j = 2 * i ; j <= 100000 ; j += i ) { ans [ j ] = ( ans [ j ] * i ) % mod ; } } } static long productOfProperDivi ( int num ) { return ans [ num ] ; } public static void main ( String [ ] args ) { Arrays . fill ( ans , 1 ) ; preCompute ( ) ; int queries = 5 ; int [ ] a = { 4 , 6 , 8 , 16 , 36 } ; for ( int i = 0 ; i < queries ; i ++ ) { System . out . print ( productOfProperDivi ( a [ i ] ) + " , ▁ " ) ; } } }
Highest power of 2 that divides the LCM of first N Natural numbers . | Java implementation of the approach ; Function to find LCM of first N natural numbers ; Initialize result ; Ans contains LCM of 1 , 2 , 3 , . . i after i 'th iteration ; Function to find the highest power of 2 which divides LCM of first n natural numbers ; Find lcm of first N natural numbers ; To store the highest required power of 2 ; Counting number of consecutive zeros from the end in the given binary String ; Driver code
import java . util . * ; class GFG { static int findlcm ( int n ) { int ans = 1 ; for ( int i = 1 ; i <= n ; i ++ ) ans = ( ( ( i * ans ) ) / ( __gcd ( i , ans ) ) ) ; return ans ; } static int highestPower ( int n ) { int lcm = findlcm ( n ) ; int ans = 0 ; for ( int i = 1 ; ; i ++ ) { int x = ( int ) Math . pow ( 2 , i ) ; if ( lcm % x == 0 ) { ans = i ; } if ( x > n ) break ; } return ans ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int n = 15 ; System . out . print ( highestPower ( n ) ) ; } }
Highest power of 2 that divides the LCM of first N Natural numbers . | Java implementation of the approach ; Function to find the highest power of 2 which divides LCM of first n natural numbers ; Driver code
class GFG { static int highestPower ( int n ) { return ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) ; } public static void main ( String [ ] args ) { int n = 15 ; System . out . println ( highestPower ( n ) ) ; } }
Check if number can be made prime by deleting a single digit | Java implementation to check if a number becomes prime by deleting any digit ; Function to check if N is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to delete character at index i from given String str ; Deletes character at position 4 ; Function to check if a number becomes prime by deleting any digit ; Converting the number to String ; length of String ; number should not be of single digit ; Loop to find all numbers after deleting a single digit ; Deleting ith character from the String ; converting String to int ; Driver Code
import java . util . * ; class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static String deleteIth ( String str , int i ) { str = str . substring ( 0 , i ) + str . substring ( i + 1 ) ; return str ; } static boolean isPrimePossible ( int N ) { String s = String . valueOf ( N ) ; int l = s . length ( ) ; if ( l < 2 ) return false ; for ( int i = 0 ; i < l ; i ++ ) { String str = deleteIth ( s , i ) ; int num = Integer . valueOf ( str ) ; if ( isPrime ( num ) ) return true ; } return false ; } public static void main ( String [ ] args ) { int N = 610 ; if ( isPrimePossible ( N ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Check if a number has an odd count of odd divisors and even count of even divisors | Java implementation of the above approach ; Function to find the count of even and odd factors of N ; Loop runs till square root ; Condition to check if the even factors of the number N is is even and count of odd factors is odd ; Driver Code
import java . util . * ; class GFG { static void checkFactors ( long N ) { long ev_count = 0 , od_count = 0 ; for ( long i = 1 ; i <= Math . sqrt ( N ) + 1 ; i ++ ) { if ( N % i == 0 ) { if ( i == N / i ) { if ( i % 2 == 0 ) ev_count += 1 ; else od_count += 1 ; } else { if ( i % 2 == 0 ) ev_count += 1 ; else od_count += 1 ; if ( ( N / i ) % 2 == 0 ) ev_count += 1 ; else od_count += 1 ; } } } if ( ev_count % 2 == 0 && od_count % 2 == 1 ) System . out . print ( " Yes " + "NEW_LINE"); else System . out . print ( " No " + "NEW_LINE"); } public static void main ( String [ ] args ) { long N = 36 ; checkFactors ( N ) ; } }
Count of distinct permutations of length N having no similar adjacent characters | Java program to implement the above approach ; Function to print the number of permutations possible ; Driver Code
class GFG { static int countofPermutations ( int N ) { return ( int ) ( 3 * Math . pow ( 2 , N - 1 ) ) ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . print ( countofPermutations ( N ) ) ; } }
Find two distinct numbers such that their LCM lies in given range | Java program for the above approach ; Function to find two distinct numbers X and Y s . t . their LCM lies between L and R and X , Y are minimum possible ; Check if 2 * L lies in range L , R ; Print the answer ; Driver Code ; Given value of ranges ; Function call
import java . io . * ; class GFG { static void answer ( int L , int R ) { if ( 2 * L <= R ) System . out . println ( L + " , ▁ " + ( 2 * L ) ) ; else System . out . println ( " - 1" ) ; } public static void main ( String [ ] args ) { int L = 3 , R = 8 ; answer ( L , R ) ; } }
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 ) ; } }