Check if all array elements are pairwise co | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE ll GCD ( ll a , ll b ) { if ( a == 0 ) return b ; return GCD ( b % a , a ) ; } ll LCM ( ll a , ll b ) { return ( a * b ) / GCD ( a , b ) ; } void checkPairwiseCoPrime ( int A [ ] , int n ) { ll prod = 1 ; ll lcm = 1 ; for ( int i = 0 ; i < n ; i ++ ) { prod *= A [ i ] ; lcm = LCM ( A [ i ] , lcm ) ; } if ( prod == lcm ) cout << " Yes " << endl ; else cout << " No " << endl ; } int main ( ) { int A [ ] = { 2 , 3 , 5 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; checkPairwiseCoPrime ( A , n ) ; }