Skip to content

Commit 4035cda

Browse files
Reaching Points
1 parent c71f3bb commit 4035cda

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ My accepted leetcode solutions to some of the common interview problems.
235235
- [Global and Local Inversions](problems/src/math/GlobalAndLocalInversions.java) (Medium)
236236
- [Solve the Equation](problems/src/math/SolveTheEquation.java) (Medium)
237237
- [Couples Holding Hands](problems/src/math/CouplesHoldingHands.java) (Hard)
238+
- [Reaching Points](problems/src/math/ReachingPoints.java) (Hard)
238239

239240
#### [Reservoir Sampling](problems/src/reservoir_sampling)
240241

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

0 commit comments

Comments
 (0)