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

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:

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 reached` `// 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 reached 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 reached 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 print Yes` `    ``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 reached 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(log(min(N, K)) for recursive stack space.

### Using a loop and modulo arithmetic:

Approach :

• Initialize a variable current to A.
• Start a loop that runs N times:
•  Compute the next position next as current + K modulo N.
• If next is equal to B, return “Yes”.
•  Otherwise, update current to next.
• Return “No” if the loop completes without finding B.

## C++

 `#include `   `using` `namespace` `std;`   `string can_reach(``int` `N, ``int` `A, ``int` `B, ``int` `K) {` `    ``int` `current = A;` `    ``for` `(``int` `i = 0; i < N; ++i) {` `        ``int` `next = (current + K) % N;` `        ``if` `(next == B) {` `            ``return` `"Yes"``;` `        ``}` `        ``current = next;` `    ``}` `    ``return` `"No"``;` `}`   `int` `main() {` `    ``// Sample Input 1` `    ``int` `N1 = 5;` `    ``int` `A1 = 2;` `    ``int` `B1 = 1;` `    ``int` `K1 = 2;` `    ``cout << can_reach(N1, A1, B1, K1) << endl; ``// Output: Yes`   `    ``// Sample Input 2` `    ``int` `N2 = 9;` `    ``int` `A2 = 6;` `    ``int` `B2 = 5;` `    ``int` `K2 = 3;` `    ``cout << can_reach(N2, A2, B2, K2) << endl; ``// Output: No`   `    ``return` `0;` `}`

## Java

 `import` `java.util.Scanner;`   `public` `class` `Main {`   `    ``// Function to check if position B can be reached from A with step K on a circular track of size N` `    ``public` `static` `String canReach(``int` `N, ``int` `A, ``int` `B, ``int` `K) {` `        ``int` `current = A;` `        ``for` `(``int` `i = ``0``; i < N; ++i) {` `            ``int` `next = (current + K) % N;` `            ``if` `(next == B) {` `                ``return` `"Yes"``;` `            ``}` `            ``current = next;` `        ``}` `        ``return` `"No"``;` `    ``}`   `    ``public` `static` `void` `main(String[] args) {` `        ``// Sample Input 1` `        ``int` `N1 = ``5``;` `        ``int` `A1 = ``2``;` `        ``int` `B1 = ``1``;` `        ``int` `K1 = ``2``;` `        ``System.out.println(canReach(N1, A1, B1, K1)); ``// Output: Yes`   `        ``// Sample Input 2` `        ``int` `N2 = ``9``;` `        ``int` `A2 = ``6``;` `        ``int` `B2 = ``5``;` `        ``int` `K2 = ``3``;` `        ``System.out.println(canReach(N2, A2, B2, K2)); ``// Output: No` `    ``}` `}`

## Python3

 `def` `can_reach(N, A, B, K):` `    ``current ``=` `A` `    ``for` `i ``in` `range``(N):` `        ``next` `=` `(current ``+` `K) ``%` `N` `        ``if` `next` `=``=` `B:` `            ``return` `"Yes"` `        ``current ``=` `next` `    ``return` `"No"`   `# Sample Input 1` `N ``=` `5` `A ``=` `2` `B ``=` `1` `K ``=` `2` `print``(can_reach(N, A, B, K)) ``# Output: Yes`   `# Sample Input 2` `N ``=` `9` `A ``=` `6` `B ``=` `5` `K ``=` `3` `print``(can_reach(N, A, B, K)) ``# Output: No`

## C#

 `using` `System;`   `class` `Program` `{` `    ``static` `string` `CanReach(``int` `N, ``int` `A, ``int` `B, ``int` `K)` `    ``{` `        ``int` `current = A;` `        ``for` `(``int` `i = 0; i < N; i++)` `        ``{` `            ``int` `next = (current + K) % N;` `            ``if` `(next == B)` `            ``{` `                ``return` `"Yes"``;` `            ``}` `            ``current = next;` `        ``}` `        ``return` `"No"``;` `    ``}`   `    ``static` `void` `Main(``string``[] args)` `    ``{` `        ``// Sample Input 1` `        ``int` `N1 = 5;` `        ``int` `A1 = 2;` `        ``int` `B1 = 1;` `        ``int` `K1 = 2;` `        ``Console.WriteLine(CanReach(N1, A1, B1, K1)); ``// Output: Yes`   `        ``// Sample Input 2` `        ``int` `N2 = 9;` `        ``int` `A2 = 6;` `        ``int` `B2 = 5;` `        ``int` `K2 = 3;` `        ``Console.WriteLine(CanReach(N2, A2, B2, K2)); ``// Output: No` `    ``}` `}`

## Javascript

 `// Function to determine if it's possible to reach point B from point A` `// by moving K steps in a circular path of size N.` `function` `canReach(N, A, B, K) {` `    ``let current = A;`   `    ``for` `(let i = 0; i < N; i++) {` `        ``// Calculate the next point by moving K steps in the circular path.` `        ``let next = (current + K) % N;`   `        ``// If the next point is equal to B, it's possible to reach B from A.` `        ``if` `(next === B) {` `            ``return` `"Yes"``;` `        ``}`   `        ``// Update the current point for the next iteration.` `        ``current = next;` `    ``}`   `    ``// If the loop completes without finding B, it's not possible to reach B from A.` `    ``return` `"No"``;` `}`   `// Sample Input 1` `const N1 = 5;` `const A1 = 2;` `const B1 = 1;` `const K1 = 2;` `console.log(canReach(N1, A1, B1, K1)); ``// Output: Yes`   `// Sample Input 2` `const N2 = 9;` `const A2 = 6;` `const B2 = 5;` `const K2 = 3;` `console.log(canReach(N2, A2, B2, K2)); ``// Output: No`

Output

```Yes
No

```

Time complexity: O(N), since we perform N iterations of the loop.
Space complexity: O(1), since we only need to store the current variable.

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next