Open In App

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:

(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++ program for the above approach
#include <bits/stdc++.h>
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 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 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# 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




<script>
 
// JavaScript program for the above approach
 
// Function to return GCD of two
// numbers a and b
function 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
function canReach(N, A, B, K)
{
 
    // Find GCD of N and K
    var gcd = GCD(N, K);
 
    // If A - B is divisible by gcd
    // then print Yes
    if (Math.abs(A - B) % gcd == 0) {
        document.write( "Yes");
    }
 
    // Otherwise
    else {
        document.write( "No");
    }
}
 
// Driver Code
var N = 5, A = 2, B = 1, K = 2;
 
// Function Call
canReach(N, A, B, K);
 
 
</script>

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 :




#include <iostream>
 
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;
}




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
    }
}




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




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
    }
}




// 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.


Article Tags :