# Check if a given value can be reached from another value in a Circular Queue by K-length jumps

• Last Updated : 18 May, 2021

Given integers N, K, A, and B, check if it is possible to reach B from A in a circular queue of integers from 1 to N placed sequentially, by jumps of K length. In each move, If it is possible, then print “Yes”. Otherwise, print “No”.

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Input: N = 5, A = 2, B = 1, K = 2
Output: Yes
Explanation: 2 -> 4 -> 1. Therefore, it is possible to reach B from A.

Input: N = 9, A = 6, B = 5, K = 3
Output: No

Approach: The idea to solve the problem is based on the following observations:

• For position A, and after t steps, the position of A is (A + K*t)%N.
• For position B, and after t steps, the position of B is (A + K*t)%N.
• It can be written as:

(A + K*t) = (N*q + B), where q is any positive integer
(A – B) = N*q – K*t

On observing the above equation (N*q – K*t) is divisible by GCD of N and K. Therefore, (A – B) is also divisible by GCD of N and K. Therefore, to reach B from A, (A – B) must be divisible by GCD(N, K).

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ` `using` `namespace` `std;`   `// Function to return GCD of two` `// numbers a and b` `int` `GCD(``int` `a, ``int` `b)` `{` `    ``// Base Case` `    ``if` `(b == 0)` `        ``return` `a;`   `    ``// Recursively Find the GCD` `    ``return` `GCD(b, a % b);` `}`   `// Function to check of B can be reaced` `// from A with a jump of K elements in` `// the circular queue` `void` `canReach(``int` `N, ``int` `A, ``int` `B, ``int` `K)` `{`   `    ``// Find GCD of N and K` `    ``int` `gcd = GCD(N, K);`   `    ``// If A - B is divisible by gcd` `    ``// then print Yes` `    ``if` `(``abs``(A - B) % gcd == 0) {` `        ``cout << ``"Yes"``;` `    ``}`   `    ``// Otherwise` `    ``else` `{` `        ``cout << ``"No"``;` `    ``}` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `N = 5, A = 2, B = 1, K = 2;`   `    ``// Function Call` `    ``canReach(N, A, B, K);`   `    ``return` `0;` `}`

## Java

 `// Java program for the` `// above approach` `import` `java.util.*;` `class` `solution{`   `// Function to return GCD ` `// of two numbers a and b` `static` `int` `GCD(``int` `a, ``int` `b)` `{` `  ``// Base Case` `  ``if` `(b == ``0``)` `    ``return` `a;`   `  ``// Recursively Find ` `  ``// the GCD` `  ``return` `GCD(b, a % b);` `}`   `// Function to check of B can` `// be reaced from A with a jump ` `// of K elements in the circular ` `// queue` `static` `void` `canReach(``int` `N, ``int` `A, ` `                     ``int` `B, ``int` `K)` `{` `  ``// Find GCD of N and K` `  ``int` `gcd = GCD(N, K);`   `  ``// If A - B is divisible` `  ``// by gcd then print Yes` `  ``if` `(Math.abs(A - B) % ` `      ``gcd == ``0``) ` `  ``{` `    ``System.out.println(``"Yes"``);` `  ``}`   `  ``// Otherwise` `  ``else` `  ``{` `    ``System.out.println(``"No"``);` `  ``}` `}`   `// Driver Code` `public` `static` `void` `main(String args[])` `{` `  ``int` `N = ``5``, A = ``2``, ` `      ``B = ``1``, K = ``2``;` `  ``// Function Call` `  ``canReach(N, A, B, K);` `}` `}`   `// This code is contributed by SURENDRA_GANGWAR`

## Python3

 `# Python3 program for the` `# above approach`   `# Function to return GCD ` `# of two numbers a and b` `def` `GCD(a, b):` `  `  `    ``# Base Case` `    ``if` `(b ``=``=` `0``):` `        ``return` `a`   `    ``# Recursively Find` `    ``# the GCD` `    ``return` `GCD(b, a ``%` `b)`   `# Function to check of B ` `# can be reaced from A ` `# with a jump of K elements ` `# in the circular queue` `def` `canReach(N, A, B, K):`   `    ``# Find GCD of N and K` `    ``gcd ``=` `GCD(N, K)`   `    ``# If A - B is divisible ` `    ``# by gcd then prYes` `    ``if` `(``abs``(A ``-` `B) ``%` `        ``gcd ``=``=` `0``):` `        ``print``(``"Yes"``)` `        `  `    ``# Otherwise    ` `    ``else``:` `        ``print``(``"No"``)`   `# Driver Code` `if` `__name__ ``=``=` `'__main__'``:` `  `  `    ``N ``=` `5` `    ``A ``=` `2` `    ``B ``=` `1` `    ``K ``=` `2`   `    ``# Function Call` `    ``canReach(N, A, B, K)`   `# This code is contributed by Mohit Kumar 29`

## C#

 `// C# program for the` `// above approach` `using` `System;`   `class` `GFG{` ` `  `// Function to return GCD ` `// of two numbers a and b` `static` `int` `GCD(``int` `a, ``int` `b)` `{` `  `  `  ``// Base Case` `  ``if` `(b == 0)` `    ``return` `a;` ` `  `  ``// Recursively Find ` `  ``// the GCD` `  ``return` `GCD(b, a % b);` `}` ` `  `// Function to check of B can` `// be reaced from A with a jump ` `// of K elements in the circular ` `// queue` `static` `void` `canReach(``int` `N, ``int` `A, ` `                     ``int` `B, ``int` `K)` `{` `  `  `  ``// Find GCD of N and K` `  ``int` `gcd = GCD(N, K);` ` `  `  ``// If A - B is divisible` `  ``// by gcd then print Yes` `  ``if` `(Math.Abs(A - B) % gcd == 0) ` `  ``{` `    ``Console.WriteLine(``"Yes"``);` `  ``}` ` `  `  ``// Otherwise` `  ``else` `  ``{` `    ``Console.WriteLine(``"No"``);` `  ``}` `}` ` `  `// Driver Code` `public` `static` `void` `Main()` `{` `  ``int` `N = 5, A = 2, ` `      ``B = 1, K = 2;` `  `  `  ``// Function Call` `  ``canReach(N, A, B, K);` `}` `}`   `// This code is contributed by code_hunt`

## Javascript

 ``

Output:

`Yes`

Time Complexity: O(log(min(N, K))
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Recommended Articles
Page :