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

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

Suppose we have a coordinate point (x, y) and another value n. We have to check whether we can move from (0, 0) to (x, y) using n steps or not. We can move any of the four directions left, right, up and down.

So, if the input is like p = (2, 1) n = 3, then the output will be True we can move two step to the right then one step to the up direction.

To solve this, we will follow these steps −

• if n >= |x of p| + |y of p| and (n -(|x of p| + |y of p|)) is even, then
• return True
• return False

## Example

Let us see the following implementation to get better understanding −

Live Demo

def solve(p, n):
if n >= abs(p) + abs(p) and (n - (abs(p) + abs(p))) % 2 == 0:
return True
return False
p = (2, 1)
n = 3
print(solve(p, n))

## Input

(2, 1), 3

## Output

True