XOR of array elements whose modular inverse with a given number exists | Function to return the gcd of a & b ; Base Case ; Recursively calculate GCD ; Function to print the Bitwise XOR of elements of arr [ ] if gcd ( arr [ i ] , M ) is 1 ; Initialize xor ; Traversing the array ; GCD of M and arr [ i ] ; If GCD is 1 , update xor ; Print xor ; Given array arr [ ] ; Given number M ; Size of the array ; Function Call
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function countInverse ( arr , N , M ) { var XOR = 0 ; for ( var i = 0 ; i < N ; i ++ ) { var gcdOfMandelement = gcd ( M , arr [ i ] ) ; if ( gcdOfMandelement == 1 ) { XOR ^= arr [ i ] ; } } document . write ( XOR ) ; } var arr = [ 1 , 2 , 3 ] ; var M = 4 ; var N = arr . length ; countInverse ( arr , N , M ) ;