Check if it is possible to move from (a, 0) to (b, 0) with given jumps
Last Updated :
17 Apr, 2023
Given two points, i.e. (a, 0) to (b, 0). The task is to check whether it is possible to move from (a,0) to (b,0) or not. One can move as (a, 0), (a+x, 0), (a+x+1, 0), (a, 2*x, 0), (a, 2*x+1, 0)......
Examples:
Input: a = 3, x = 10, b = 4
Output: No
Input: a = 3, x = 2, b = 5
Output: Yes
Approach: An answer will be possible if:
- a + n*x = b, where n is a non-negative integer.
- a + n*x + 1 = b where n is a positive integer.
So,
(b - a) / x is an integer or (b - a - 1) / x is an integer
(b - a) % x = 0 or (b - a - 1) % x = 0
Below is the implementation of the above approach:
C++
// CPP program to move from
// (a, 0) to (b, 0) with given jumps
#include <bits/stdc++.h>
using namespace std;
// Function to check if it is possible
bool Move(int a, int x, int b)
{
if ((((b - a) % x == 0) || ((b - a - 1) % x == 0) && a + 1 != b) && b >= a)
return true;
return false;
}
// Driver code
int main()
{
int a = 3, x = 2, b = 7;
// function call
if (Move(a, x, b))
cout << "Yes";
else
cout << "No";
}
C
// C program to move form
// (a, 0) to (b, 0) with given jumps
#include <stdio.h>
#include <stdbool.h>
// Function to check if it is possible
bool Move(int a, int x, int b)
{
if ((((b - a) % x == 0) || ((b - a - 1) % x == 0) && a + 1 != b) && b >= a)
return true;
return false;
}
// Driver code
int main()
{
int a = 3, x = 2, b = 7;
// function call
if (Move(a, x, b))
printf("Yes");
else
printf("No");
}
// This code is contributed by kothavvsaakash.
Java
// Java program to move form
// (a, 0) to (b, 0) with given jumps
import java.io.*;
class GFG {
// Function to check if it is possible
static boolean Move(int a, int x, int b)
{
if ((((b - a) % x == 0) || ((b - a - 1) % x == 0) && a + 1 != b) && b >= a)
return true;
return false;
}
// Driver code
public static void main (String[] args) {
int a = 3, x = 2, b = 7;
// function call
if (Move(a, x, b))
System.out.println( "Yes");
else
System.out.println( "No");
}
}
//This code is contributed by shs..
Python 3
# Python 3 program to move form
# (a, 0) to (b, 0) with given jumps
# Function to check if it
# is possible
def Move(a, x, b):
if ((((b - a) % x == 0) or
((b - a - 1) % x == 0) and
a + 1 != b) and b >= a):
return True
return False
# Driver code
if __name__ == "__main__":
a = 3
x = 2
b = 7
# function call
if (Move(a, x, b)):
print("Yes")
else:
print("No")
# This code is contributed
# by ChitraNayal
C#
// C# program to move form
// (a, 0) to (b, 0) with given jumps
using System;
class GFG
{
// Function to check if it is possible
static bool Move(int a, int x, int b)
{
if ((((b - a) % x == 0) ||
((b - a - 1) % x == 0) &&
a + 1 != b) && b >= a)
return true;
return false;
}
// Driver code
public static void Main ()
{
int a = 3, x = 2, b = 7;
// function call
if (Move(a, x, b))
Console.WriteLine( "Yes");
else
Console.WriteLine( "No");
}
}
// This code is contributed
// by inder_verma
PHP
<?php
// PHP program to move form
// (a, 0) to (b, 0) with given jumps
// Function to check if it is possible
function Move($a, $x, $b)
{
if (((($b - $a) % $x == 0) ||
(($b - $a - 1) % $x == 0) &&
$a + 1 != $b) && $b >= $a)
return true;
return false;
}
// Driver code
$a = 3; $x = 2; $b = 7;
// function call
if (Move($a, $x, $b))
echo "Yes";
else
echo"No";
// This code is contributed
// by anuj_67
?>
JavaScript
<script>
// Javascript program to move form
// (a, 0) to (b, 0) with given jumps
// Function to check if it is possible
function Move(a,x,b)
{
if ((((b - a) % x == 0) || ((b - a - 1) % x == 0)
&& a + 1 != b) && b >= a)
return true;
return false;
}
// Driver code
let a = 3, x = 2, b = 7;
// function call
if (Move(a, x, b))
document.write( "Yes");
else
document.write( "No");
// This code is contributed by avanitrachhadiya2155
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Check if it is possible to reach M from 0 by given paths Given an array arr[] consisting of N pairs of integers, where each pair (a, b) represents a path from a to b, the task is to check if it is possible to reach M from 0 using the given paths in the array arr[]. If it is possible, then print "Yes". Otherwise, print "No". Examples: Input: arr[] = {{0, 2
8 min read
Check if it is possible to reach (X, Y) from (1, 0) by given steps Given two positive integers X and Y, the task is to check if it is possible to reach (X, Y) from (1, 0) by the given steps. In each step, possible moves from any cell (a, b) are (a, b + a) or (a + b, b). Print "Yes" if possible. Otherwise, print "No". Examples: Input: X = 2, Y = 7Output: YesExplanat
5 min read
Check if it is possible to move from (0, 0) to (X, Y) in exactly K steps Given a point (X, Y) in a 2-D plane and an integer K, the task is to check whether it is possible to move from (0, 0) to the given point (X, Y) in exactly K moves. In a single move, the positions that are reachable from (X, Y) are (X, Y + 1), (X, Y - 1), (X + 1, Y) and (X - 1, Y).Examples: Input: X
4 min read
Check if it is possible to reach (X, Y) from (1, 1) by given steps Given two integers X and Y, the task is to check if it is possible to reach (X, Y) from (1, 1) by the following possible moves: From a point (a, b) such that b > a, move to the to point (a, b - a).From a point (a, b) such that a > b, move to the to point (a - b, b).Move from any point (a, b) t
6 min read
Check if it is possible to reach a number by making jumps of two given length Given a starting position 'k' and two jump sizes 'd1' and 'd2', our task is to find the minimum number of jumps needed to reach 'x' if it is possible.At any position P, we are allowed to jump to positions : P + d1 and P - d1P + d2 and P - d2 Examples: Input : k = 10, d1 = 4, d2 = 6 and x = 8 Output
9 min read