-
Notifications
You must be signed in to change notification settings - Fork 820
/
ReachingPoints.java
84 lines (78 loc) · 2.33 KB
/
ReachingPoints.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* (C) 2024 YourCompanyName */
package math;
/**
* Created by gouthamvidyapradhan on 14/07/2018. A move consists of taking a point (x, y) and
* transforming it to either (x, x+y) or (x+y, y).
*
* <p>Given a starting point (sx, sy) and a target point (tx, ty), return True if and only if a
* sequence of moves exists to transform the point (sx, sy) to (tx, ty). Otherwise, return False.
*
* <p>Examples: Input: sx = 1, sy = 1, tx = 3, ty = 5 Output: True Explanation: One series of moves
* that transforms the starting point to the target is: (1, 1) -> (1, 2) (1, 2) -> (3, 2) (3, 2) ->
* (3, 5)
*
* <p>Input: sx = 1, sy = 1, tx = 2, ty = 2 Output: False
*
* <p>Input: sx = 1, sy = 1, tx = 1, ty = 1 Output: True
*
* <p>Note:
*
* <p>sx, sy, tx, ty will all be integers in the range [1, 10^9].
*
* <p>Solution: Start from the target, reduce the target value to start value. If at any stage the
* target value goes below start value then there exist no solution hence return false.
*/
public class ReachingPoints {
class Pair {
int x, y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
/**
* Main method
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
System.out.println(new ReachingPoints().reachingPoints(1, 1, 153, 10));
}
public boolean reachingPoints(int sx, int sy, int tx, int ty) {
Pair target = new Pair(tx, ty);
Pair start = new Pair(sx, sy);
while (true) {
if (start.x == target.x && start.y == target.y) {
return true;
} else if (start.x > target.x || start.y > target.y || target.x == target.y) {
return false;
} else if (start.x == target.x) {
int t = target.y - start.y;
return (t % target.x) == 0;
} else if (start.y == target.y) {
int t = target.x - start.x;
return (t % target.y) == 0;
} else {
if (target.x > target.y) {
int[] R = reduce(target.x, target.y);
target.x = R[0];
target.y = R[1];
} else {
int[] R = reduce(target.y, target.x);
target.x = R[1];
target.y = R[0];
}
}
}
}
private int[] reduce(int x, int y) {
int t = x - y;
int q = t / y;
x -= (y * q);
if ((t % y) != 0) {
x -= y;
}
return new int[] {x, y};
}
}