Check if it is possible to reach (x, y) from origin in exactly Z steps using only plus movements

• Last Updated : 05 Nov, 2021

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 Possible
Input: x = 10, y = 15, z = 25
Output: Possible

Approach:

1. The shortest path from origin to (x, y), is |x|+|y|.
2. So, it is clear that if Z less than |x|+|y|, then we can’t reach (x, y) from origin in exactly Z steps.
3. 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.
4. 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 using namespace std; // Function to check if it is possible to// reach (x, y) from origin in exactly z stepsvoid 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 Codeint 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 approachclass GFG{ // Function to check if it is possible// to reach (x, y) from origin in// exactly z stepsstatic 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 Codepublic 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 stepsdef 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 Codeif __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 approachusing System;class GFG{ // Function to check if it is possible// to reach (x, y) from origin in// exactly z stepsstatic 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 Codepublic 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

Javascript


Output:
Not Possible

Time Complexity: O(1)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up