Check whether the exchange is possible or not | C ++ implementation of the approach ; Function that returns true if the exchange is possible ; Find the GCD of the array elements ; If the exchange is possible ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossible ( int arr [ ] , int n , int p ) { int gcd = 0 ; for ( int i = 0 ; i < n ; i ++ ) gcd = __gcd ( gcd , arr [ i ] ) ; if ( p % gcd == 0 ) return true ; return false ; } int main ( ) { int arr [ ] = { 6 , 9 } ; int n = sizeof ( arr ) / sizeof ( int ) ; int p = 3 ; if ( isPossible ( arr , n , p ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }