# Check if it is possible to move from (0, 0) to (X, Y) in exactly K steps

Given a point **(X, Y)** in a 2-D plane and an integer **K**, the task is to check whether it is possible to move from **(0, 0)** to the given point **(X, Y)** in exactly **K** moves. In a single move, the positions that are reachable from **(X, Y)** are **(X, Y + 1)**, **(X, Y – 1)**, **(X + 1, Y)** and **(X – 1, Y)**.

**Examples:**

Input:X = 0, Y = 0, K = 2

Output:Yes

Move 1: (0, 0) -> (0, 1)

Move 2: (0, 1) -> (0, 0)

Input:X = 5, Y = 8, K = 20

Output:No

**Approach:** It is clear that the shortest path to reach **(X, Y)** from **(0, 0)** will be **minMoves = (|X| + |Y|)**. So, if **K < minMoves** then it is impossible to reach **(X, Y)** but if **K ≥ minMoves** then after reaching **(X, Y)** in **minMoves** number of moves the remaining **(K – minMoves)** number of moves have to be even in order to remain at that point for the rest of the moves.

So it is possible to reach **(X, Y)** from **(0, 0)** only if **K ≥ (|X| + |Y|) and (K – (|X| + |Y|)) % 2 = 0**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that returns true if it is ` `// possible to move from (0, 0) to ` `// (x, y) in exactly k moves ` `bool` `isPossible(` `int` `x, ` `int` `y, ` `int` `k) ` `{ ` ` ` `// Minimum moves required ` ` ` `int` `minMoves = ` `abs` `(x) + ` `abs` `(y); ` ` ` ` ` `// If possible ` ` ` `if` `(k >= minMoves && (k - minMoves) % 2 == 0) ` ` ` `return` `true` `; ` ` ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `x = 5, y = 8, k = 20; ` ` ` ` ` `if` `(isPossible(x, y, k)) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` ` ` `// Function that returns true if it is ` ` ` `// possible to move from (0, 0) to ` ` ` `// (x, y) in exactly k moves ` ` ` `static` `boolean` `isPossible(` `int` `x, ` `int` `y, ` `int` `k) ` ` ` `{ ` ` ` `// Minimum moves required ` ` ` `int` `minMoves = Math.abs(x) + Math.abs(y); ` ` ` ` ` `// If possible ` ` ` `if` `(k >= minMoves && (k - minMoves) % ` `2` `== ` `0` `) ` ` ` `return` `true` `; ` ` ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `x = ` `5` `, y = ` `8` `, k = ` `20` `; ` ` ` ` ` `if` `(isPossible(x, y, k)) ` ` ` `System.out.println(` `"Yes"` `); ` ` ` `else` ` ` `System.out.println(` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function that returns true if it is ` `# possible to move from (0, 0) to ` `# (x, y) in exactly k moves ` `def` `isPossible(x, y, k): ` ` ` ` ` `# Minimum moves required ` ` ` `minMoves ` `=` `abs` `(x) ` `+` `abs` `(y) ` ` ` ` ` `# If possible ` ` ` `if` `(k >` `=` `minMoves ` `and` `(k ` `-` `minMoves) ` `%` `2` `=` `=` `0` `): ` ` ` `return` `True` ` ` ` ` `return` `False` ` ` `# Driver code ` `x ` `=` `5` `y ` `=` `8` `k ` `=` `20` ` ` `if` `(isPossible(x, y, k)): ` ` ` `print` `(` `"Yes"` `) ` `else` `: ` ` ` `print` `(` `"No"` `) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` `class` `GFG ` `{ ` ` ` ` ` `// Function that returns true if it is ` ` ` `// possible to move from (0, 0) to ` ` ` `// (x, y) in exactly k moves ` ` ` `static` `bool` `isPossible(` `int` `x, ` `int` `y, ` `int` `k) ` ` ` `{ ` ` ` `// Minimum moves required ` ` ` `int` `minMoves = Math.Abs(x) + Math.Abs(y); ` ` ` ` ` `// If possible ` ` ` `if` `(k >= minMoves && (k - minMoves) % 2 == 0) ` ` ` `return` `true` `; ` ` ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `x = 5, y = 8, k = 20; ` ` ` ` ` `if` `(isPossible(x, y, k)) ` ` ` `Console.Write(` `"Yes"` `); ` ` ` `else` ` ` `Console.Write(` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by Nidhi ` |

*chevron_right*

*filter_none*

**Output:**

No

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Check if it is possible to move from (0, 0) to (x, y) in N steps
- Check if a king can move a valid move or not when N nights are there in a modified chessboard
- Minimize steps required to move all 1's in a matrix to a given index
- Check if it is possible to move from (a, 0) to (b, 0) with given jumps
- Check if it is possible to reach (x, y) from origin in exactly Z steps using only plus movements
- Check if possible to move from given coordinate to desired coordinate
- Number of cells a queen can move with obstacles on the chessborad
- Count the total number of squares that can be visited by Bishop in one move
- Maximum money that can be withdrawn in two steps
- Find the number of stair steps
- Largest number N which can be reduced to 0 in K steps
- Minimum steps to color the tree with given colors
- Generate array with minimum sum which can be deleted in P steps
- Number of steps required to reach point (x,y) from (0,0) using zig-zag way
- Probability of reaching a point with 2 or 3 steps at a time
- Minimum steps to get 1 at the center of a binary matrix
- Rat in a Maze with multiple steps or jump allowed
- Find the minimum number of steps to reach M from N
- Minimum steps to reach N from 1 by multiplying each step by 2, 3, 4 or 5
- Count the minimum steps to reach 0 from the given integer N

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.