Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if it is possible to reach a number by making jumps of two given length in Python
We need to find the minimum number of steps to reach position q from starting position p by making jumps of length d1 or d2 in either direction (left or right).
The key insight is that we can only reach a target if the difference between start and end positions is divisible by the GCD of the two jump distances.
Algorithm Steps
To solve this problem, we follow these steps ?
- Calculate GCD of d1 and d2
- Check if (p - q) is divisible by GCD - if not, return -1
- Use BFS to find minimum steps
- Track visited positions to avoid cycles
- Try all four possible moves: +d1, +d2, -d1, -d2
Example
Let's implement the solution with p = 5, q = 10, d1 = 4, d2 = 3 ?
from math import gcd
from collections import deque
def solve(p, d1, d2, q):
gcd_res = gcd(d1, d2)
if (p - q) % gcd_res != 0:
return -1
que = deque()
visited = set()
que.appendleft([p, 0])
visited.add(p)
while len(que) > 0:
pair = que.pop()
point, step = pair[0], pair[1]
if point == q:
return step
if point + d1 not in visited:
que.appendleft([point + d1, step + 1])
visited.add(point + d1)
if point + d2 not in visited:
que.appendleft([point + d2, step + 1])
visited.add(point + d2)
if point - d1 not in visited:
que.appendleft([point - d1, step + 1])
visited.add(point - d1)
if point - d2 not in visited:
que.appendleft([point - d2, step + 1])
visited.add(point - d2)
# Test the function
p = 5
q = 10
d1 = 4
d2 = 3
result = solve(p, d1, d2, q)
print(f"Minimum steps to reach from {p} to {q}: {result}")
Minimum steps to reach from 5 to 10: 3
How It Works
The solution path for our example is ?
- Start at position 5
- Jump right 4 units ? position 9 (step 1)
- Jump right 4 units ? position 13 (step 2)
- Jump left 3 units ? position 10 (step 3)
Alternative Test Case
Let's test an impossible case ?
from math import gcd
from collections import deque
def solve(p, d1, d2, q):
gcd_res = gcd(d1, d2)
if (p - q) % gcd_res != 0:
return -1
que = deque()
visited = set()
que.appendleft([p, 0])
visited.add(p)
while len(que) > 0:
pair = que.pop()
point, step = pair[0], pair[1]
if point == q:
return step
if point + d1 not in visited:
que.appendleft([point + d1, step + 1])
visited.add(point + d1)
if point + d2 not in visited:
que.appendleft([point + d2, step + 1])
visited.add(point + d2)
if point - d1 not in visited:
que.appendleft([point - d1, step + 1])
visited.add(point - d1)
if point - d2 not in visited:
que.appendleft([point - d2, step + 1])
visited.add(point - d2)
# Test impossible case: GCD of 4 and 6 is 2, but (0-1) % 2 != 0
p = 0
q = 1
d1 = 4
d2 = 6
result = solve(p, d1, d2, q)
print(f"Result for impossible case: {result}")
Result for impossible case: -1
Conclusion
This algorithm uses BFS to find the minimum steps, first checking if the target is reachable using GCD theory. The time complexity is dependent on the search space, which can be limited by setting boundaries to avoid infinite exploration.
