Reach a Target Pair by Repeatedly Adding One Coordinate to the Other
Company: Virtu
Role: Quantitative Researcher
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Online Assessment
You start with an ordered pair of positive integers `(sx, sy)`. In one move you replace the current pair `(x, y)` with either `(x, x + y)` or `(x + y, y)`.
Return whether some sequence of zero or more moves turns `(sx, sy)` into exactly `(tx, ty)`.
### Function Signature
```python
def can_reach(sx: int, sy: int, tx: int, ty: int) -> bool:
```
### Rules
- The pair is ordered: `(2, 3)` and `(3, 2)` are different pairs.
- Zero moves are allowed, so the answer is `True` whenever `(sx, sy) == (tx, ty)`.
- Return `True` if `(tx, ty)` is reachable and `False` otherwise.
### Constraints
- `1 <= sx, sy, tx, ty <= 10^9`
- All input values fit in a 32-bit signed integer.
### Examples
**Example 1**
```text
Input: sx = 1, sy = 1, tx = 3, ty = 5
Output: True
```
One sequence is `(1, 1) -> (1, 2) -> (3, 2) -> (3, 5)`.
**Example 2**
```text
Input: sx = 1, sy = 1, tx = 2, ty = 2
Output: False
```
Starting from `(1, 1)`, the only reachable pairs with both coordinates at most `2` are `(1, 1)`, `(1, 2)` and `(2, 1)`.
**Example 3**
```text
Input: sx = 1, sy = 1, tx = 1, ty = 1
Output: True
```
Overview: Starting from a pair of positive integers, each move adds one coordinate to the other. Decide whether an exact target pair can be reached. Tests reasoning about reachability when values go up to one billion and simulating every forward move is far too slow.
Read the full Virtu Quantitative Researcher interview experience this question came from
You start with an ordered pair of positive integers `(sx, sy)`. In one move you replace the current pair `(x, y)` with either `(x, x + y)` or `(x + y, y)`.
Implement `can_reach(sx, sy, tx, ty)`, which returns whether some sequence of zero or more moves turns `(sx, sy)` into exactly `(tx, ty)`: `True` if `(tx, ty)` is reachable and `False` otherwise (`true` / `false` in JavaScript, Java and C++).
### Rules
- The pair is ordered: `(2, 3)` and `(3, 2)` are different pairs.
- Zero moves are allowed, so the answer is `True` whenever `(sx, sy) == (tx, ty)`.
### Constraints
- `1 <= sx, sy, tx, ty <= 10^9`
- All input values fit in a 32-bit signed integer. The result is a boolean, so no value in this problem exceeds `2^31 - 1`; a 32-bit `int` is sufficient in Java and C++.
### Examples
**Example 1**
```text
Input: sx = 1, sy = 1, tx = 3, ty = 5
Output: True
```
One sequence is `(1, 1) -> (1, 2) -> (3, 2) -> (3, 5)`.
**Example 2**
```text
Input: sx = 1, sy = 1, tx = 2, ty = 2
Output: False
```
Starting from `(1, 1)`, the only reachable pairs with both coordinates at most `2` are `(1, 1)`, `(1, 2)` and `(2, 1)`.
Constraints
- 1 <= sx, sy, tx, ty <= 10^9
- All input values fit in a 32-bit signed integer.
Examples
Input: (1, 1, 1, 1)
Expected Output: True
Explanation: Minimum values; start equals target, so zero moves suffice.
Input: (1, 1, 3, 5)
Expected Output: True
Explanation: Example 1: (1, 1) -> (1, 2) -> (3, 2) -> (3, 5).
Hints
- Each move changes exactly one coordinate and strictly increases it. What does that imply when a target coordinate is smaller than the matching start coordinate?
- Ask which pairs could have produced (tx, ty) in a single move, given that both coordinates always stay positive.
- Values reach 10^9, so applying moves one at a time can take far too many steps; look for a way to account for a long run of identical moves at once.