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).
Input: X = 0, Y = 0, K = 2
Move 1: (0, 0) -> (0, 1)
Move 2: (0, 1) -> (0, 0)
Input: X = 5, Y = 8, K = 20
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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if it is possible to move from (0, 0) to (x, y) in N steps
- Check if it is possible to reach (x, y) from origin in exactly Z steps using only plus movements
- 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 possible to move from given coordinate to desired coordinate
- 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 (1, 0) by given steps
- Check if it is possible to reach (X, Y) from (1, 1) by given steps
- Minimum steps required to convert X to Y where a binary matrix represents the possible conversions
- Maximum of smallest possible area that can get with exactly k cut of given rectangular
- Find if it is possible to choose subarray that it contains exactly K even integers
- Maximum possible prime divisors that can exist in numbers having exactly N divisors
- Check whether a number has exactly three distinct factors or not
- Check if each element of the given array is the product of exactly K prime numbers
- Queries to check whether all the elements can be made positive by flipping signs exactly K times
- Check if original Array is retained after performing XOR with M exactly K times
- Check if a number exists having exactly N factors and K prime factors
- Check if a number can be expressed as a product of exactly K prime divisors
- Check if there exists a number with X factors out of which exactly K are prime
- Check if sum of exactly K elements of the Array can be odd or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.