Check if a given value can be reached from another value in a Circular Queue by K-length jumps
Last Updated :
07 Nov, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int GCD( int a, int b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
void canReach( int N, int A, int B, int K)
{
int gcd = GCD(N, K);
if ( abs (A - B) % gcd == 0) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
}
int main()
{
int N = 5, A = 2, B = 1, K = 2;
canReach(N, A, B, K);
return 0;
}
|
Java
import java.util.*;
class solution{
static int GCD( int a, int b)
{
if (b == 0 )
return a;
return GCD(b, a % b);
}
static void canReach( int N, int A,
int B, int K)
{
int gcd = GCD(N, K);
if (Math.abs(A - B) %
gcd == 0 )
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
public static void main(String args[])
{
int N = 5 , A = 2 ,
B = 1 , K = 2 ;
canReach(N, A, B, K);
}
}
|
Python3
def GCD(a, b):
if (b = = 0 ):
return a
return GCD(b, a % b)
def canReach(N, A, B, K):
gcd = GCD(N, K)
if ( abs (A - B) %
gcd = = 0 ):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
N = 5
A = 2
B = 1
K = 2
canReach(N, A, B, K)
|
C#
using System;
class GFG{
static int GCD( int a, int b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
static void canReach( int N, int A,
int B, int K)
{
int gcd = GCD(N, K);
if (Math.Abs(A - B) % gcd == 0)
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
public static void Main()
{
int N = 5, A = 2,
B = 1, K = 2;
canReach(N, A, B, K);
}
}
|
Javascript
<script>
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( "Yes" );
}
else {
document.write( "No" );
}
}
var N = 5, A = 2, B = 1, K = 2;
canReach(N, A, B, K);
</script>
|
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 <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() {
int N1 = 5;
int A1 = 2;
int B1 = 1;
int K1 = 2;
cout << can_reach(N1, A1, B1, K1) << endl;
int N2 = 9;
int A2 = 6;
int B2 = 5;
int K2 = 3;
cout << can_reach(N2, A2, B2, K2) << endl;
return 0;
}
|
Java
import java.util.Scanner;
public class Main {
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) {
int N1 = 5 ;
int A1 = 2 ;
int B1 = 1 ;
int K1 = 2 ;
System.out.println(canReach(N1, A1, B1, K1));
int N2 = 9 ;
int A2 = 6 ;
int B2 = 5 ;
int K2 = 3 ;
System.out.println(canReach(N2, A2, B2, K2));
}
}
|
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"
N = 5
A = 2
B = 1
K = 2
print (can_reach(N, A, B, K))
N = 9
A = 6
B = 5
K = 3
print (can_reach(N, A, B, K))
|
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)
{
int N1 = 5;
int A1 = 2;
int B1 = 1;
int K1 = 2;
Console.WriteLine(CanReach(N1, A1, B1, K1));
int N2 = 9;
int A2 = 6;
int B2 = 5;
int K2 = 3;
Console.WriteLine(CanReach(N2, A2, B2, K2));
}
}
|
Javascript
function canReach(N, A, B, K) {
let current = A;
for (let i = 0; i < N; i++) {
let next = (current + K) % N;
if (next === B) {
return "Yes" ;
}
current = next;
}
return "No" ;
}
const N1 = 5;
const A1 = 2;
const B1 = 1;
const K1 = 2;
console.log(canReach(N1, A1, B1, K1));
const N2 = 9;
const A2 = 6;
const B2 = 5;
const K2 = 3;
console.log(canReach(N2, A2, B2, K2));
|
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.
Share your thoughts in the comments
Please Login to comment...