Check if it is possible to move from (a, 0) to (b, 0) with given jumps

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



  1. a + n*x = b where n is a non-negative integer.
  2. 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 above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to move form
// (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";
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Yes


My Personal Notes arrow_drop_up

pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.