Check if possible to move from given coordinate to desired coordinate
Last Updated :
06 Jul, 2022
Given two coordinates (x, y) and (a, b). Find if it is possible to reach (x, y) from (a, b).
Only possible moves from any coordinate (i, j) are
- (i-j, j)
- (i, i-j)
- (i+j, j)
- (i, i+j)
Given x, y, a, b can be negative.
Examples:
Input : (x, y) = (1, 1) and (a, b) = (2, 3).
Output : Yes.
(1, 1) -> (2, 1) -> (2, 3).
Input : (x, y) = (2, 1) and (a, b) = (2, 3).
Output : Yes.
Input : (x, y) = (35, 15) and (a, b) = (20, 25).
Output : Yes.
(35, 15) -> (20, 15) -> (5, 15) -> (5, 10) -> (5, 5) ->
(10, 5) -> (15, 5) -> (20, 5) -> (20, 25)
If we take a closer look at the problem, we can notice that the moves are similar steps of Euclidean algorithm for finding GCD. So, it is only possible to reach coordinate (a, b) from (x, y) if GCD of x, y is equal to GCD of a, b. Otherwise, it is not possible.
Let GCD of (x, y) be gcd. From (x, y), we can reach (gcd, gcd) and from this point, we can reach to (a, b) if and only if GCD of 'a' and 'b' is also gcd.
Below is the implementation of this approach:
C++
// C++ program to check if it is possible to reach
// (a, b) from (x, y).
#include <bits/stdc++.h>
using namespace std;
// Returns GCD of i and j
int gcd(int i, int j)
{
if (i == j)
return i;
if (i > j)
return gcd(i - j, j);
return gcd(i, j - i);
}
// Returns true if it is possible to go to (a, b)
// from (x, y)
bool ispossible(int x, int y, int a, int b)
{
// Find absolute values of all as sign doesn't
// matter.
x = abs(x), y = abs(y), a = abs(a), b = abs(b);
// If gcd is equal then it is possible to reach.
// Else not possible.
return (gcd(x, y) == gcd(a, b));
}
// Driven Program
int main()
{
// Converting coordinate into positive integer
int x = 35, y = 15;
int a = 20, b = 25;
(ispossible(x, y, a, b)) ? (cout << "Yes") : (cout << "No");
return 0;
}
Java
// Java program to check if it is possible
// to reach (a, b) from (x, y).
class GFG {
// Returns GCD of i and j
static int gcd(int i, int j)
{
if (i == j)
return i;
if (i > j)
return gcd(i - j, j);
return gcd(i, j - i);
}
// Returns true if it is possible to go to (a, b)
// from (x, y)
static boolean ispossible(int x, int y, int a, int b)
{
// Find absolute values of all as
// sign doesn't matter.
x = Math.abs(x);
y = Math.abs(y);
a = Math.abs(a);
b = Math.abs(b);
// If gcd is equal then it is possible to reach.
// Else not possible.
return (gcd(x, y) == gcd(a, b));
}
// Driver code
public static void main(String[] args)
{
// Converting coordinate into positive integer
int x = 35, y = 15;
int a = 20, b = 25;
if (ispossible(x, y, a, b))
System.out.print("Yes");
else
System.out.print("No");
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python program to check if it is possible to reach
# (a, b) from (x, y).
# Returns GCD of i and j
def gcd(i, j):
if (i == j):
return i
if (i > j):
return gcd(i - j, j)
return gcd(i, j - i)
# Returns true if it is possible to go to (a, b)
# from (x, y)
def ispossible(x, y, a, b):
# Find absolute values of all as sign doesn't
# matter.
x, y, a, b = abs(x), abs(y), abs(a), abs(b)
# If gcd is equal then it is possible to reach.
# Else not possible.
return (gcd(x, y) == gcd(a, b))
# Driven Program
# Converting coordinate into positive integer
x, y = 35, 15
a, b = 20, 25
if(ispossible(x, y, a, b)):
print ("Yes")
else:
print ("No")
# Contributed by Afzal Ansari
C#
// C# program to check if it is possible
// to reach (a, b) from (x, y).
using System;
class GFG {
// Returns GCD of i and j
static int gcd(int i, int j)
{
if (i == j)
return i;
if (i > j)
return gcd(i - j, j);
return gcd(i, j - i);
}
// Returns true if it is possible
// to go to (a, b) from (x, y)
static bool ispossible(int x, int y,
int a, int b)
{
// Find absolute values of all as
// sign doesn't matter.
x = Math.Abs(x);
y = Math.Abs(y);
a = Math.Abs(a);
b = Math.Abs(b);
// If gcd is equal then it is possible
// to reach. Else not possible.
return (gcd(x, y) == gcd(a, b));
}
// Driver code
public static void Main()
{
// Converting coordinate
// into positive integer
int x = 35, y = 15;
int a = 20, b = 25;
if (ispossible(x, y, a, b))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by nitin mittal.
PHP
<?php
// PHP program to check if it
// is possible to reach
// (a, b) from (x, y).
// Returns GCD of i and j
function gcd($i, $j)
{
if ($i == $j)
return $i;
if ($i > $j)
return gcd($i - $j, $j);
return gcd($i, $j - $i);
}
// Returns true if it is
// possible to go to (a, b)
// from (x, y)
function ispossible($x, $y, $a, $b)
{
// Find absolute values
// of all as sign doesn't
// matter.
$x = abs($x);
$y = abs($y);
$a = abs($a);
$b = abs($b);
// If gcd is equal then
// it is possible to reach.
// Else not possible.
return (gcd($x, $y) == gcd($a, $b));
}
// Driver Code
{
// Converting coordinate
// into positive integer
$x = 35; $y = 15;
$a = 20; $b = 25;
if (ispossible($x, $y, $a, $b))
echo( "Yes");
else
echo( "No");
return 0;
}
// This code is contributed by nitin mittal.
?>
JavaScript
<script>
// javascript program to check if it is possible
// to reach (a, b) from (x, y).
// Returns GCD of i and j
function gcd(i , j) {
if (i == j)
return i;
if (i > j)
return gcd(i - j, j);
return gcd(i, j - i);
}
// Returns true if it is possible to go to (a, b)
// from (x, y)
function ispossible(x , y , a , b)
{
// Find absolute values of all as
// sign doesn't matter.
x = Math.abs(x);
y = Math.abs(y);
a = Math.abs(a);
b = Math.abs(b);
// If gcd is equal then it is possible to reach.
// Else not possible.
return (gcd(x, y) == gcd(a, b));
}
// Driver code
// Converting coordinate into positive integer
var x = 35, y = 15;
var a = 20, b = 25;
if (ispossible(x, y, a, b))
document.write("Yes");
else
document.write("No");
// This code is contributed by todaysgaurav
</script>
Time Complexity: O(min(x, y) + min(a, b)), where x, y, a and b are the given integers.
Auxiliary Space: O(min(x, y) + min(a, b)), space required due to the recursion stack.
Similar Reads
GCD (Greatest Common Divisor) Practice Problems for Competitive Programming
GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest positive integer that divides both of the numbers.GCD of Two NumbersFastest Way to Compute GCDThe fastest way to find the Greatest Common Divisor (GCD) of two numbers is by using the Euclidean algorithm. The E
4 min read
Program to Find GCD or HCF of Two Numbers
Given two numbers a and b, the task is to find the GCD of the two numbers.Note: The GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that divides both of them. Examples:Input: a = 20, b = 28Output: 4Explanation: The factors of 20 are 1, 2, 4, 5, 10 an
15+ min read
Check if two numbers are co-prime or not
Two numbers A and B are said to be Co-Prime or mutually prime if the Greatest Common Divisor of them is 1. You have been given two numbers A and B, find if they are Co-prime or not.Examples : Input : 2 3Output : Co-PrimeInput : 4 8Output : Not Co-PrimeThe idea is simple, we find GCD of two numbers a
5 min read
GCD of more than two (or array) numbers
Given an array arr[] of non-negative numbers, the task is to find GCD of all the array elements. In a previous post we find GCD of two number.Examples:Input: arr[] = [1, 2, 3]Output: 1Input: arr[] = [2, 4, 6, 8]Output: 2Using Recursive GCDThe GCD of three or more numbers equals the product of the pr
11 min read
Program to find LCM of two numbers
LCM of two numbers is the smallest number which can be divided by both numbers. Input : a = 12, b = 18Output : 3636 is the smallest number divisible by both 12 and 18Input : a = 5, b = 11Output : 5555 is the smallest number divisible by both 5 and 11[Naive Approach] Using Conditional Loop This appro
8 min read
LCM of given array elements
In this article, we will learn how to find the LCM of given array elements.Given an array of n numbers, find the LCM of it. Example:Input : {1, 2, 8, 3}Output : 24LCM of 1, 2, 8 and 3 is 24Input : {2, 7, 3, 9, 4}Output : 252Table of Content[Naive Approach] Iterative LCM Calculation - O(n * log(min(a
14 min read
Find the other number when LCM and HCF given
Given a number A and L.C.M and H.C.F. The task is to determine the other number B. Examples: Input: A = 10, Lcm = 10, Hcf = 50. Output: B = 50 Input: A = 5, Lcm = 25, Hcf = 4. Output: B = 20 Formula: A * B = LCM * HCF B = (LCM * HCF)/AExample : A = 15, B = 12 HCF = 3, LCM = 60 We can see that 3 * 60
4 min read
Minimum insertions to make a Co-prime array
Given an array of N elements, find the minimum number of insertions to convert the given array into a co-prime array. Print the resultant array also.Co-prime Array : An array in which every pair of adjacent elements are co-primes. i.e, gcd(a, b) = 1 . Examples : Input : A[] = {2, 7, 28}Output : 1Exp
6 min read
Find the minimum possible health of the winning player
Given an array health[] where health[i] is the health of the ith player in a game, any player can attack any other player in the game. The health of the player being attacked will be reduced by the amount of health the attacking player has. The task is to find the minimum possible health of the winn
4 min read
Minimum squares to evenly cut a rectangle
Given a rectangular sheet of length l and width w. we need to divide this sheet into square sheets such that the number of square sheets should be as minimum as possible.Examples: Input :l= 4 w=6 Output :6 We can form squares with side of 1 unit, But the number of squares will be 24, this is not min
4 min read