# Reaching Points in C++

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

Suppose we have a starting points (sx, sy), and target point (tx, ty), we have to check whether a sequence of moves exists from the start point to the end point. Here move consists of taking a point (x, y) and transforming it to either (x, x+y) or (x+y, y).

So if the inputs are (1, 1) and (4,5), then the answer will be true, this is because move (1,1) to (2,1), then (3,1), then (4,1), then (4,5).

To solve this, we will follow these steps −

• while tx > sx and ty > sy, do −
• if tx > ty, then −
• tx := tx mod ty
• Otherwise
• ty := ty mod tx
• return (true when sx is same as tx AND sy <= ty AND (ty - sy) mod tx is same as 0) OR (sy is same as ty AND x >= sx AND (tx - sx) mod ty is 0)

Let us see the following implementation to get better understanding −

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
bool reachingPoints(int sx, int sy, int tx, int ty) {
while(tx > sx && ty > sy){
if(tx > ty){
tx %= ty;
}else ty %= tx;
}
return (sx == tx && sy <= ty && (ty - sy) % tx == 0) || (sy == ty && tx >= sx && (tx - sx) % ty == 0);
}
};
main(){
Solution ob;
cout << (ob.reachingPoints(1,1,4,5));
}

## Input

1
1
4
5

## Output

1