text: Check if a given value can be reached from another value in a Circular Queue by K | Function to return GCD of two numbers a and b ; Base Case ; Recursively Find the GCD ; Function to check of B can be reaced from A with a jump of K elements in the circular queue ; Find GCD of N and K ; If A - B is divisible by gcd then print Yes ; Otherwise ; Driver Code ; Function Call
code: function GCD ( a , b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } function canReach ( N , A , B , K ) { var gcd = GCD ( N , K ) ; if ( Math . abs ( A - B ) % gcd == 0 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var N = 5 , A = 2 , B = 1 , K = 2 ; canReach ( N , A , B , K ) ;