Check whether the exchange is possible or not | Recursive function to return gcd of a and b ; Function that returns true if the exchange is possible ; Find the GCD of the array elements ; If the exchange is possible ; Driver code
function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } function isPossible ( arr , n , p ) { let gcd = 0 ; for ( let i = 0 ; i < n ; i ++ ) gcd = __gcd ( gcd , arr [ i ] ) ; if ( p % gcd == 0 ) return true ; return false ; } let arr = [ 6 , 9 ] ; let n = arr . length ; let p = 3 ; if ( isPossible ( arr , n , p ) ) document . write ( " " ) ; else document . write ( " " ) ;