Given a point (x, y), the task is to check if it is possible to reach from origin to (x, y) in exactly Z steps. From a given point (x, y) we can only moves in four direction left(x – 1, y), right(x + 1, y), up(x, y + 1) and down(x, y – 1).
Examples:
Input: x = 5, y = 5, z = 11
Output: Not PossibleInput: x = 10, y = 15, z = 25
Output: Possible
Approach:
- The shortest path from origin to (x, y), is |x|+|y|.
- So, it is clear that if Z less than |x|+|y|, then we can’t reach (x, y) from origin in exactly Z steps.
- If the number of steps is not less than |x|+|y| then, we have to check below two conditions to check if we can reach to (x, y) or not:
- If Z ≥ |x| + |y|, and
- If (Z – |x| + |y|)%2 is 0.
- For the second conditions in the above step, if we reach (x, y), we can take two more steps such as (x, y)–>(x, y+1)–>(x, y) to come back to the same position (x, y). And this is possible only if different between them is even.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to check if it is possible to // reach (x, y) from origin in exactly z steps void possibleToReach(int x, int y, int z) { // Condition if we can't reach in Z steps if (z < abs(x) + abs(y) || (z - abs(x) - abs(y)) % 2) { cout << "Not Possible" << endl; } else cout << "Possible" << endl; } // Driver Code int main() { // Destination point coordinate int x = 5, y = 5; // Number of steps allowed int z = 11; // Function Call possibleToReach(x, y, z); return 0; } |
Java
// Java program for the above approach class GFG{ // Function to check if it is possible // to reach (x, y) from origin in // exactly z steps static void possibleToReach(int x, int y, int z) { // Condition if we can't reach in Z steps if (z < Math.abs(x) + Math.abs(y) || (z - Math.abs(x) - Math.abs(y)) % 2 == 1) { System.out.print("Not Possible" + "\n"); } else System.out.print("Possible" + "\n"); } // Driver Code public static void main(String[] args) { // Destination point coordinate int x = 5, y = 5; // Number of steps allowed int z = 11; // Function Call possibleToReach(x, y, z); } } // This code is contributed by 29AjayKumar |
Python3
# Python3 program for the above approach # Function to check if it is possible to # reach (x, y) from origin in exactly z steps def possibleToReach(x, y, z): #Condition if we can't reach in Z steps if (z < abs(x) + abs(y) or (z - abs(x) - abs(y)) % 2): print("Not Possible") else: print("Possible") # Driver Code if __name__ == '__main__': # Destination pocoordinate x = 5 y = 5 # Number of steps allowed z = 11 # Function call possibleToReach(x, y, z) # This code is contributed by mohit kumar 29 |
C#
// C# program for the above approach using System; class GFG{ // Function to check if it is possible // to reach (x, y) from origin in // exactly z steps static void possibleToReach(int x, int y, int z) { // Condition if we can't reach in Z steps if (z < Math.Abs(x) + Math.Abs(y) || (z - Math.Abs(x) - Math.Abs(y)) % 2 == 1) { Console.Write("Not Possible" + "\n"); } else Console.Write("Possible" + "\n"); } // Driver Code public static void Main(String[] args) { // Destination point coordinate int x = 5, y = 5; // Number of steps allowed int z = 11; // Function Call possibleToReach(x, y, z); } } // This code is contributed by 29AjayKumar |
Not Possible
Time Complexity: O(1)
Auxiliary Space: O(1)
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.
Recommended Posts:
- Count of all possible ways to reach a target by a Knight
- Check if Pascal's Triangle is possible with a complete layer by using numbers upto N
- How to check if string contains only digits in Java
- Check if an Array can be Sorted by picking only the corner Array elements
- Make array elements equal in Minimum Steps
- Minimum number of steps needed to remove the substring K from given string
- Maximize the sum of differences of consecutive elements after removing exactly K elements
- Construct a string that has exactly K subsequences from given string
- Smallest String consisting of a String S exactly K times as a Substring
- Check if it is possible to create a matrix such that every row has A 1s and every column has B 1s
- Longest substring of only 4's from the first N characters of the infinite string
- Sum of the digits of square of the given number which has only 1's as its digits
- Make sum of all subarrays of length K equal by only inserting elements
- Count of substrings containing only the given character
- Maximum possible time that can be formed from four digits
- Count of strings possible by replacing two consecutive same character with new character
- Longest palindromic string possible after removal of a substring
- Count all possible unique sum of series K, K+1, K+2, K+3, K+4, ..., K+N
- Check if a string can be formed from another string using given constraints
- How to check string is alphanumeric or not using Regular Expression
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.

