Modify array such that the array does not contain any common divisors other than 1 | Java program for the above approach ; Function to check if it is possible to modify the array such that there is no common factor between array elements except 1 ; Stores GCD of the array ; Calculate GCD of the array ; If the current divisor is smaller than X ; Divide GCD by the current divisor ; If possible ; Print the modified array ; Otherwise ; Calculating gcd ; Driver Code ; Given array ; Size of the array
class GFG { static void checkCommonDivisor ( int [ ] arr , int N , int X ) { int G = 0 ; for ( int i = 0 ; i < N ; i ++ ) { G = gcd ( G , arr [ i ] ) ; } int copy_G = G ; for ( int divisor = 2 ; divisor <= X ; divisor ++ ) { while ( G % divisor == 0 ) { G = G / divisor ; } } if ( G <= X ) { System . out . println ( " Yes " ) ; for ( int i = 0 ; i < N ; i ++ ) System . out . print ( ( arr [ i ] / copy_G ) + " ▁ " ) ; System . out . println ( ) ; } else System . out . println ( " No " ) ; } static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 6 , 15 , 6 } ; int X = 6 ; int N = arr . length ; checkCommonDivisor ( arr , N , X ) ; } }