Pair Growth by Adding One Coordinate to the Other
Math & Bit ManipulationAuto-graded · 24 test casesLast updated
Decide whether a positive integer pair can grow into a target pair by repeatedly adding one coordinate to the other, using reverse-step reasoning and Euclid-style reduction.
You start with a pair of positive integers (a, b). In one step you may choose either of these updates:
- Replace the first number with the sum of both:
(x, y) -> (x + y, y). - Replace the second number with the sum of both:
(x, y) -> (x, x + y).
You may apply any number of steps (including zero) in any order.
Given the starting pair (a, b) and a target pair (c, d), return True if the target can be produced from the start, and False otherwise.
Details
- The arguments are
a,b,c,d, in that order.(a, b)is the start and(c, d)is the target. - Applying zero steps is allowed, so if
(a, b) == (c, d)the answer isTrue. - Both numbers stay positive at all times, and neither number ever decreases.
- Return a boolean.
Constraints
1 <= a, b, c, d <= 1000- All inputs are integers.
Examples
Example 1
Input: can_grow_to_pair(1, 1, 3, 5)
Output: True
Example 2
Input: can_grow_to_pair(1, 1, 2, 2)
Output: False
Example 3
Input: can_grow_to_pair(1, 2, 5, 3)
Output: False
Example 4
Input: can_grow_to_pair(2, 3, 2, 3)
Output: True
Hints
Hint 1
Going forward you have two choices every step, but going backward from the target there is only one possible previous pair. Ask which coordinate must have been updated last.
Hint 2
If c > d, the previous pair must have been (c - d, d); if d > c, it must have been (c, d - c). If c == d, no step could have produced it, so it is reachable only if it already equals the start.
Hint 3
Repeated subtraction is just the Euclidean algorithm, so you can use modulo to speed it up. Be careful: when one coordinate already matches the start, do not jump with modulo. Instead check whether the other coordinate differs from its start value by a multiple of the matching coordinate.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Searching forward from (a, b) branches into two options each step. Searching backward from (c, d) does not branch at all.
Every forward step writes the sum into one coordinate, and since all values are positive, that sum is strictly larger than the other coordinate. So in any pair produced by a step, the larger coordinate is the one just written:
- If
c > d, the previous pair was(c - d, d). - If
d > c, the previous pair was(c, d - c). - If
c == d, no step can have produced it, so it is only reachable when it is the start itself.
The backward path is therefore unique. We walk it until we hit (a, b) (answer True) or one coordinate falls below its start value (answer False), since coordinates never decrease going forward.
Speeding it up
Subtracting one step at a time is slow for pairs like (1000, 1). Collapsing repeated subtraction into c %= d (or d %= c) is exactly the Euclidean algorithm.
There is one subtlety: modulo can jump past the start. If d == b, the second coordinate stays fixed while we subtract d from c, so the only question is whether c can land exactly on a. That holds exactly when (c - a) % d == 0. The symmetric check (d - b) % c == 0 applies when c == a. When the fixed coordinate does not match the start, the plain modulo is safe, because the intermediate pairs cannot equal (a, b).
Why it is correct
- The predecessor is forced, so any valid forward sequence appears reversed as our backward walk. If the walk never reaches
(a, b), no sequence exists. - Coordinates are non-decreasing going forward, so dropping below
aorbrules out success. - Modulo skips only states whose fixed coordinate differs from the start's, which can never match
(a, b). The one case where it could matter is handled by the explicit divisibility check.
Complexity
- Time: O(log(max(c, d))), as in the Euclidean algorithm.
- Space: O(1).
Reference solution
def can_grow_to_pair(a: int, b: int, c: int, d: int) -> bool:
while c >= a and d >= b:
if c == a and d == b:
return True
if c > d:
# The previous pair was (c - d, d). If the second coordinate
# already equals b, it stays fixed, so the first coordinate must
# reach a by subtracting d a whole number of times.
if d == b:
return (c - a) % d == 0
c %= d
else:
# The previous pair was (c, d - c). Symmetric shortcut when the
# first coordinate already equals a.
if c == a:
return (d - b) % c == 0
d %= c
return c == a and d == b