Check if it is possible to reach (x, y) from origin in exactly Z steps using only plus movements
Last Updated :
18 Apr, 2023
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:
- 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 difference 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 point coordinate
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
JavaScript
<script>
// javascript program for the above approach
// Function to check if it is possible
// to reach (x, y) from origin in
// exactly z steps
function possibleToReach(x , y , 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)
{
document.write("Not Possible" + "\n");
}
else
document.write("Possible" + "\n");
}
// Driver Code
// Destination point coordinate
var x = 5, y = 5;
// Number of steps allowed
var z = 11;
// Function Call
possibleToReach(x, y, z);
// This code is contributed by Amit Katiyar
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
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
4 min read
Check if it is possible to reach (X, Y) from origin such that in each ith move increment x or y coordinate with 3^i Given two positive integers X and Y, the task is to find whether a point (X, Y) can be reached from the point (0, 0) such that in each ith move x-coordinate or y-coordinate can be incremented by 3i. If it is possible then print Yes. Otherwise, print No. Examples: Input: X = 1, Y = 3Output: YesExplan
10 min read
Check if it is possible to reach (X, Y) from (1, 0) by given steps Given two positive integers X and Y, the task is to check if it is possible to reach (X, Y) from (1, 0) by the given steps. In each step, possible moves from any cell (a, b) are (a, b + a) or (a + b, b). Print "Yes" if possible. Otherwise, print "No". Examples: Input: X = 2, Y = 7Output: YesExplanat
5 min read
Check if it is possible to reach (X, Y) from (1, 1) by given steps Given two integers X and Y, the task is to check if it is possible to reach (X, Y) from (1, 1) by the following possible moves: From a point (a, b) such that b > a, move to the to point (a, b - a).From a point (a, b) such that a > b, move to the to point (a - b, b).Move from any point (a, b) t
6 min read
Check if it is possible to reach any point on the circumference of a given circle from origin Given a string S representing a sequence of moves(L, R, U, and D) and an integer R representing the radius of a circle whose center is the origin (0, 0), the task is to check if it is possible to reach any point on the circumference of the given circle from the origin by choosing any subsequence of
10 min read