text: Modify array such that the array does not contain any common divisors other than 1 | 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 ; Prvar the modified array ; Otherwise ; Calculating gcd ; Given array ; Size of the array
code: function checkCommonDivisor ( arr , N , X ) { var G = 0 ; for ( i = 0 ; i < N ; i ++ ) { G = gcd ( G , arr [ i ] ) ; } var copy_G = G ; for ( divisor = 2 ; divisor <= X ; divisor ++ ) { while ( G % divisor == 0 ) { G = G / divisor ; } } if ( G <= X ) { document . write ( " " ) ; for ( i = 0 ; i < N ; i ++ ) document . write ( ( arr [ i ] / copy_G ) + " " ) ; document . write ( ) ; } else document . write ( " " ) ; } function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } var arr = [ 6 , 15 , 6 ] ; var X = 6 ; var N = arr . length ; checkCommonDivisor ( arr , N , X ) ;