Check if all array elements are pairwise co | Java program for the above approach ; Function to calculate GCD ; Function to calculate LCM ; Function to check if all elements in the array are pairwise coprime ; Initialize variables ; Iterate over the array ; Calculate product of array elements ; Calculate LCM of array elements ; If the product of array elements is equal to LCM of the array ; Driver Code ; Function call
import java . util . * ; import java . lang . * ; class GFG { static long GCD ( long a , long b ) { if ( a == 0 ) return b ; return GCD ( b % a , a ) ; } static long LCM ( long a , long b ) { return ( a * b ) / GCD ( a , b ) ; } static void checkPairwiseCoPrime ( int A [ ] , int n ) { long prod = 1 ; long lcm = 1 ; for ( int i = 0 ; i < n ; i ++ ) { prod *= A [ i ] ; lcm = LCM ( A [ i ] , lcm ) ; } if ( prod == lcm ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 3 , 5 } ; int n = A . length ; checkPairwiseCoPrime ( A , n ) ; } }