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

Given a point (x, y). Find whether it is possible or not to move from (0, 0) to (x, y) in exactly n steps. 4 types of steps are valid, you can move from a point (a, b) to either of (a, b+1), (a, b-1), (a-1, b), (a+1, b)

**Examples**:

Input: x = 0, y = 0, n = 2 Output: POSSIBLE Input: x = 1, y = 1, n = 3 Output: IMPOSSIBLE

**Approach **:

In the shortest path, one can move from (0, 0) to (x, y) in |x| + |y|. So, it is not possible to move from (0, 0) to (x, y) in less than |x| + |y| steps. After reaching one can take two more steps as (x, y) -> (x, y+1) -> (x, y).

So, it is possible if

n >= |x| + |y|and( n-( |x| + |y| ) ) % 2 = 0.

Below is the implementation of the above approach:

## C++

`// CPP program to check whether it is possible ` `// or not to move from (0, 0) to (x, y) ` `// in exactly n steps ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check whether it is possible ` `// or not to move from (0, 0) to (x, y) ` `// in exactly n steps ` `bool` `Arrive(` `int` `a, ` `int` `b, ` `int` `n) ` `{ ` ` ` `if` `(n >= ` `abs` `(a) + ` `abs` `(b) and (n - (` `abs` `(a) + ` `abs` `(b))) % 2 == 0) ` ` ` `return` `true` `; ` ` ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a = 5, b = 5, n = 11; ` ` ` ` ` `if` `(Arrive(a, b, n)) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to check whether it is possible ` `// or not to move from (0, 0) to (x, y) ` `// in exactly n steps ` `import` `java.io.*; ` ` ` `public` `class` `GFG { ` ` ` `// Function to check whether it is possible ` `// or not to move from (0, 0) to (x, y) ` `// in exactly n steps ` `static` `boolean` `Arrive(` `int` `a, ` `int` `b, ` `int` `n) ` `{ ` ` ` `if` `(n >= Math.abs(a) + Math.abs(b) && (n - (Math.abs(a) + Math.abs(b))) % ` `2` `== ` `0` `) ` ` ` `return` `true` `; ` ` ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `return` `0` `; ` `} ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` ` ` `int` `a = ` `5` `, b = ` `5` `, n = ` `11` `; ` ` ` ` ` `if` `(Arrive(a, b, n)) ` ` ` `System.out.println( ` `"Yes"` `); ` ` ` `else` ` ` `System.out.println( ` `"No"` `); ` ` ` `} ` `} ` `//This code is contributed by shs.. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to check whether ` `# it is possible or not to move from ` `# (0, 0) to (x, y) in exactly n steps ` ` ` `# Function to check whether it is ` `# possible or not to move from ` `# (0, 0) to (x, y) in exactly n steps ` `def` `Arrive(a, b, n): ` ` ` ` ` `if` `(n >` `=` `abs` `(a) ` `+` `abs` `(b) ` `and` ` ` `(n ` `-` `(` `abs` `(a) ` `+` `abs` `(b))) ` `%` `2` `=` `=` `0` `): ` ` ` `return` `True` ` ` ` ` `return` `False` ` ` `# Driver code ` `a ` `=` `5` `b ` `=` `5` `n ` `=` `11` ` ` `if` `(Arrive(a, b, n)): ` ` ` `print` `(` `"Yes"` `) ` `else` `: ` ` ` `print` `(` `"No"` `) ` ` ` `# This code is contributed ` `# by Yatin Gupta ` |

*chevron_right*

*filter_none*

## C#

`// C# program to check whether ` `// it is possible or not to ` `// move from (0, 0) to (x, y) ` `// in exactly n steps ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to check whether it ` `// is possible or not to move ` `// from (0, 0) to (x, y) in ` `// exactly n steps ` `static` `bool` `Arrive(` `int` `a, ` `int` `b, ` `int` `n) ` `{ ` ` ` `if` `(n >= Math.Abs(a) + Math.Abs(b) && ` ` ` `(n - (Math.Abs(a) + Math.Abs(b))) % 2 == 0) ` ` ` `return` `true` `; ` ` ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main () ` `{ ` ` ` `int` `a = 5, b = 5, n = 11; ` ` ` ` ` `if` `(Arrive(a, b, n)) ` ` ` `Console.WriteLine( ` `"Yes"` `); ` ` ` `else` ` ` `Console.WriteLine( ` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by shashank ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to check whether ` `// it is possible or not to move ` `// from (0, 0) to (x, y) in exactly n steps ` ` ` `// Function to check whether it ` `// is possible or not to move ` `// from (0, 0) to (x, y) in exactly n steps ` `function` `Arrive(` `$a` `, ` `$b` `, ` `$n` `) ` `{ ` ` ` `if` `(` `$n` `>= ` `abs` `(` `$a` `) + ` `abs` `(` `$b` `) ` `and` ` ` `(` `$n` `- (` `abs` `(` `$a` `) + ` `abs` `(` `$b` `))) % 2 == 0) ` ` ` `return` `true; ` ` ` ` ` `return` `false; ` `} ` ` ` `// Driver code ` `$a` `= 5; ` `$b` `= 5; ` `$n` `= 11; ` ` ` `if` `(Arrive(` `$a` `, ` `$b` `, ` `$n` `)) ` ` ` `echo` `"Yes"` `; ` `else` ` ` `echo` `"No"` `; ` ` ` `// This code is contributed ` `// by Akanksha Rai(Abby_akku) ` |

*chevron_right*

*filter_none*

**Output:**

No

## Recommended Posts:

- Check if a king can move a valid move or not when N nights are there in a modified chessboard
- Check if it is possible to move from (a, 0) to (b, 0) with given jumps
- Check if possible to move from given coordinate to desired coordinate
- Number of blocks in a chessboard a knight can move to in exactly k moves
- Minimum revolutions to move center of a circle to a target
- Find the number of stair steps
- Count the total number of squares that can be visited by Bishop in one move
- Number of odd and even results for every value of x in range [min, max] after performing N steps
- Count minimum steps to get the given desired array
- Number of steps to convert to prime factors
- Probability of reaching a point with 2 or 3 steps at a time
- Position after taking N steps to the right and left in an alternate manner
- Steps to reduce N to zero by subtracting its most significant digit at every step
- Minimum steps to come back to starting point in a circular tour
- Minimum steps needed to cover a sequence of points on an infinite grid

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.