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.
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:
This article is contributed by Anuj Chauhan(anuj0503). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if a king can move a valid move or not when N nights are there in a modified chessboard
- Count minimum steps to get the given desired array
- Check if it is possible to move from (a, 0) to (b, 0) with given jumps
- Number of possible Triangles in a Cartesian coordinate system
- Check if it is possible to move from (0, 0) to (x, y) in N steps
- Check if it is possible to move from (0, 0) to (X, Y) in exactly K steps
- Check if a sequence of path visits any coordinate twice or not
- Count of Squares that are parallel to the coordinate axis from the given set of N points
- Finding Quadrant of a Coordinate with respect to a Circle
- Move matrix elements in given direction and add elements with same value
- Minimize steps required to move all 1's in a matrix to a given index
- Number of cells a queen can move with obstacles on the chessborad
- Count the total number of squares that can be visited by Bishop in one move
- Minimum number of operations to move all uppercase characters before all lower case characters
- Minimum moves taken to move coin of each cell to any one cell of Matrix
- Minimum LCM and GCD possible among all possible sub-arrays
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Check if possible to cross the matrix with given power
- Check if it is possible to make the given matrix increasing matrix or not
- Check if it is possible to create a polygon with a given angle
Improved By : nitin mittal