Check if it is possible to move from (0, 0) to (x, y) in N steps in Python

Given a coordinate point (x, y) and a number of steps n, we need to determine if it's possible to move from the origin (0, 0) to the target point using exactly n steps. We can move in four directions: left, right, up, and down.

For example, if we want to reach point (2, 1) in 3 steps, we can move two steps right and one step up, which is possible.

Understanding the Problem

The key insight is that we need at least the Manhattan distance (|x| + |y|) steps to reach the target. Any extra steps must be used in pairs to return to the same position ?

Origin (0,0) Target (2,1) Right ? Right ? Up Manhattan distance: |2| + |1| = 3

Algorithm

The solution follows these conditions ?

  • We need at least |x| + |y| steps (Manhattan distance)
  • If we have extra steps, they must be even in number to return to the same position
  • If n >= |x| + |y| and (n - (|x| + |y|)) is even, then it's possible

Implementation

def can_reach_point(x, y, n):
    """
    Check if we can reach point (x, y) from (0, 0) in exactly n steps
    """
    manhattan_distance = abs(x) + abs(y)
    
    # Need at least manhattan distance steps
    if n < manhattan_distance:
        return False
    
    # Extra steps must be even to return to same position
    extra_steps = n - manhattan_distance
    return extra_steps % 2 == 0

# Test cases
test_cases = [
    ((2, 1), 3),   # True: minimum steps needed
    ((2, 1), 5),   # True: 3 minimum + 2 extra (even)
    ((2, 1), 4),   # False: 3 minimum + 1 extra (odd)
    ((3, 3), 6),   # True: exactly minimum steps
    ((1, 1), 10)   # True: 2 minimum + 8 extra (even)
]

for (x, y), steps in test_cases:
    result = can_reach_point(x, y, steps)
    print(f"Can reach ({x}, {y}) in {steps} steps: {result}")
Can reach (2, 1) in 3 steps: True
Can reach (2, 1) in 5 steps: True
Can reach (2, 1) in 4 steps: False
Can reach (3, 3) in 6 steps: True
Can reach (1, 1) in 10 steps: True

Why Extra Steps Must Be Even

If we have extra steps beyond the minimum required, we need to "waste" them while staying at the target. This can only be done by moving away and returning, which requires an even number of moves ?

def explain_solution(x, y, n):
    manhattan = abs(x) + abs(y)
    extra = n - manhattan
    
    print(f"Target: ({x}, {y})")
    print(f"Steps available: {n}")
    print(f"Minimum steps needed: {manhattan}")
    print(f"Extra steps: {extra}")
    
    if n < manhattan:
        print("Impossible: Not enough steps")
    elif extra % 2 == 0:
        print("Possible: Extra steps are even (can waste by going back and forth)")
    else:
        print("Impossible: Extra steps are odd (cannot waste efficiently)")

# Example explanations
explain_solution(2, 1, 3)
print()
explain_solution(2, 1, 4)
Target: (2, 1)
Steps available: 3
Minimum steps needed: 3
Extra steps: 0
Possible: Extra steps are even (can waste by going back and forth)

Target: (2, 1)
Steps available: 4
Minimum steps needed: 3
Extra steps: 1
Impossible: Extra steps are odd (cannot waste efficiently)

Conclusion

To check if we can reach point (x, y) in exactly n steps, we verify that n is at least the Manhattan distance and any extra steps are even. This ensures we can waste extra moves by going back and forth while ending at the target.

---
Updated on: 2026-03-25T15:11:40+05:30

311 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements