Check if all the pairs of an array are coprime with each other | Java implementation of the above approach ; Function to check if all the pairs of the array are coprime with each other or not ; Check if GCD of the pair is not equal to 1 ; All pairs are non - coprime Return false ; Function return gcd of two number ; Driver Code
import java . util . * ; import java . lang . * ; class GFG { static boolean allCoprime ( int A [ ] , int n ) { boolean all_coprime = true ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( gcd ( A [ i ] , A [ j ] ) != 1 ) { all_coprime = false ; break ; } } } return all_coprime ; } static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 5 , 11 , 7 , 19 } ; int arr_size = A . length ; if ( allCoprime ( A , arr_size ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }