text: Modify array such that the array does not contain any common divisors other than 1 | C ++ 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 ; Driver Code ; Given array ; Size of the array
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; 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 ) { cout << " Yes STRNEWLINE " ; for ( int i = 0 ; i < N ; i ++ ) cout << arr [ i ] / copy_G << " ▁ " ; cout << endl ; } else cout << " No " ; } int main ( ) { int arr [ ] = { 6 , 15 , 6 } , X = 6 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; checkCommonDivisor ( arr , N , X ) ; }