Can a Starting Pair Grow Into a Goal Pair by Summing Coordinates?
Math & Bit ManipulationAuto-graded · 25 test casesLast updated
Decide whether a goal pair is reachable from a start pair by replacing one coordinate with the sum of both, using a reverse walk with Euclid-style modulo jumps for values up to 10^9.
You are given a start pair of positive integers (a, b) and a goal pair (c, d).
A single operation takes the current pair (x, y) and replaces it with one of two pairs:
(x + y, y): the first coordinate becomes the sum of both, or(x, x + y): the second coordinate becomes the sum of both.
Determine whether (c, d) can be produced from (a, b) by applying the operation some number of times (possibly zero).
Return True if the goal pair is reachable and False otherwise.
Details
- Pairs are ordered, so
(2, 3)and(3, 2)are different pairs. - Applying zero operations is allowed. If
(a, b) == (c, d), the answer isTrue. - Both coordinates are always positive, so every operation strictly increases exactly one coordinate.
Parameters
a,b: the start pair, first and second coordinate.c,d: the goal pair, first and second coordinate.
Returns
A boolean: True if (c, d) is reachable from (a, b), otherwise False.
Constraints
1 <= a, b, c, d <= 10^9- All inputs fit in a signed 32-bit integer.
- A solution that tries every forward sequence of operations will be too slow.
Examples
Example 1
Input: can_grow_to(1, 1, 3, 5)
Output: True
Example 2
Input: can_grow_to(1, 1, 2, 2)
Output: False
Example 3
Input: can_grow_to(1, 1, 1, 1)
Output: True
Hints
Hint 1
Going forward, every pair has two children. Going backward, look at which coordinate is larger in the goal pair: how many different pairs could have produced it?
Hint 2
Each pair has at most one predecessor: subtract the smaller coordinate from the larger one. If the coordinates are equal, there is no valid predecessor. Peeling off one subtraction at a time is too slow for values near 10^9, so think about how Euclid's algorithm batches subtractions with a modulo.
Hint 3
A plain modulo can jump over the start pair. When the coordinate being reduced already matches its start value, the other coordinate must reach its start value by subtracting that fixed amount some whole number of times, which is a divisibility check. Otherwise the modulo is safe.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Searching forward branches into two options at every step, which is hopeless for values up to 10^9. Searching backward is deterministic.
Because both coordinates are positive, an operation always leaves one coordinate strictly larger than the other:
(x + y, y)has a larger first coordinate.(x, x + y)has a larger second coordinate.
So for a pair (c, d):
- If
c > d, the previous pair must have been(c - d, d). - If
d > c, the previous pair must have been(c, d - c). - If
c == d, there is no predecessor, since it would require a zero coordinate.
Each pair therefore has at most one parent, and the question becomes whether the unique backward chain from (c, d) passes through (a, b).
Speeding up the walk
Subtracting one step at a time can take about 10^9 steps, for example from (10^9, 1). When c > d, repeated subtraction of d ends at c % d, exactly like the subtraction form of Euclid's algorithm, so we can jump there directly.
The jump can overshoot the start, though. For start (3, 2) and goal (7, 2), the chain is (7,2) -> (5,2) -> (3,2), but 7 % 2 = 1 skips over 3.
- If
c > dandd == b: the second coordinate is already at its start value and can never change on a path that still reaches the start. The answer isTrueexactly whenccan reachaby subtractingdwhole times, which means(c - a) % d == 0. The loop condition guaranteesc >= a. - If
c > dandd > b:dstill has to shrink, which only happens afterchas dropped belowd. Every skipped state has second coordinated != b, so none of them is the start, and jumping toc % dis safe.
The case d > c is symmetric.
Why it is correct
- Predecessors are unique, so the goal is reachable if and only if the start lies on its backward chain.
- Going backward, coordinates never increase. Once
c < aord < b, the start can no longer be hit, so we stop withFalse. - The modulo only skips states whose fixed coordinate differs from the start's, and the divisibility check handles the one case where skipping would be wrong.
- A pair with
c == dthat is not the start is a dead end, so we returnFalse.
If the modulo produces 0, the coordinate falls below the start (which is at least 1) and the loop exits with False. This is correct because that chain would run into an equal pair.
Complexity
- Time:
O(log(max(c, d))), since each modulo step behaves like a step of Euclid's algorithm. - Space:
O(1).
Reference solution
def can_grow_to(a: int, b: int, c: int, d: int) -> bool:
# Walk backward from the goal (c, d); each pair has at most one predecessor.
while c >= a and d >= b:
if c == a and d == b:
return True
if c > d:
# Predecessor of (c, d) is (c - d, d).
if d == b:
# d stays fixed; c must reach a by subtracting d whole times.
return (c - a) % d == 0
c %= d
elif d > c:
# Predecessor of (c, d) is (c, d - c).
if c == a:
# c stays fixed; d must reach b by subtracting c whole times.
return (d - b) % c == 0
d %= c
else:
# c == d and not the start: no valid predecessor.
return False
return False