Check if it is possible to move from (0, 0) to (x, y) in N steps

25 May, 2022
Given a point (x, y). Find whether it is possible or not to move from (0, 0) to (x, y) in exactly n steps. 4 types of steps are valid, you can move from a point (a, b) to either of (a, b+1), (a, b-1), (a-1, b), (a+1, b)Examples:  
Input: x = 0, y = 0, n = 2
Output: POSSIBLE

Input: x = 1, y = 1, n = 3 
Output: IMPOSSIBLE
 
Approach :  
In the shortest path, one can move from (0, 0) to (x, y) in |x| + |y|. So, it is not possible to move from (0, 0) to (x, y) in less than |x| + |y| steps. After reaching one can take two more steps as (x, y) -> (x, y+1) -> (x, y).  
So, it is possible if  
n >= |x| + |y| and ( n-( |x| + |y| ) ) % 2 = 0.  
Below is the implementation of the above approach:  
C++
C
Java
Python3
C#
PHP
Javascript
// CPP program to check whether it is possible// or not to move from (0, 0) to (x, y)// in exactly n steps#include <bits/stdc++.h>using namespace std; // Function to check whether it is possible// or not to move from (0, 0) to (x, y)// in exactly n stepsbool Arrive(int a, int b, int n){    if (n >= abs(a) + abs(b) and (n - (abs(a) + abs(b))) % 2 == 0)        return true;     return false;} // Driver codeint main(){    int a = 5, b = 5, n = 11;     if (Arrive(a, b, n))        cout << "Yes";    else        cout << "No";     return 0;}
// C program to check whether it is possible// or not to move from (0, 0) to (x, y)// in exactly n steps#include <stdio.h>#include <stdbool.h> int abs(int a){  int abs = a;  if(abs < 0)    abs = abs * (-1);  return abs;} // Function to check whether it is possible// or not to move from (0, 0) to (x, y)// in exactly n stepsbool Arrive(int a, int b, int n){    if (n >= abs(a) + abs(b) && (n - (abs(a) + abs(b))) % 2 == 0)        return true;     return false;} // Driver codeint main(){    int a = 5, b = 5, n = 11;     if (Arrive(a, b, n))        printf("Yes");    else        printf("No");     return 0;} // This code is contributed by kothavvsaakash.
// Java program to check whether it is possible// or not to move from (0, 0) to (x, y)// in exactly n stepsimport java.io.*; public class GFG { // Function to check whether it is possible// or not to move from (0, 0) to (x, y)// in exactly n stepsstatic boolean Arrive(int a, int b, int n){    if (n >= Math.abs(a) + Math.abs(b) && (n - (Math.abs(a) + Math.abs(b))) % 2 == 0)        return true;     return false;} // Driver codeint main(){     return 0;}     public static void main (String[] args) {             int a = 5, b = 5, n = 11;     if (Arrive(a, b, n))        System.out.println( "Yes");    else        System.out.println( "No");    }}//This code is contributed by shs..
# Python3 program to check whether# it is possible or not to move from# (0, 0) to (x, y) in exactly n steps # Function to check whether it is# possible or not to move from# (0, 0) to (x, y) in exactly n stepsdef Arrive(a, b, n):         if (n >= abs(a) + abs(b) and       (n - (abs(a) + abs(b))) % 2 == 0):        return True     return False # Driver codea = 5b = 5n = 11 if (Arrive(a, b, n)):    print("Yes")else:    print("No") # This code is contributed# by Yatin Gupta    
// C# program to check whether// it is possible or not to// move from (0, 0) to (x, y)// in exactly n stepsusing System; class GFG{ // Function to check whether it// is possible or not to move// from (0, 0) to (x, y) in// exactly n stepsstatic bool Arrive(int a, int b, int n){    if (n >= Math.Abs(a) + Math.Abs(b) &&       (n - (Math.Abs(a) + Math.Abs(b))) % 2 == 0)        return true;     return false;} // Driver codepublic static void Main (){    int a = 5, b = 5, n = 11;     if (Arrive(a, b, n))        Console.WriteLine( "Yes");    else        Console.WriteLine( "No");    }} // This code is contributed by shashank
<?php// PHP program to check whether// it is possible or not to move// from (0, 0) to (x, y) in exactly n steps // Function to check whether it// is possible or not to move// from (0, 0) to (x, y) in exactly n stepsfunction Arrive($a, $b, $n){    if ($n >= abs($a) + abs($b) and       ($n - (abs($a) + abs($b))) % 2 == 0)        return true;     return false;} // Driver code$a = 5; $b = 5; $n = 11; if (Arrive($a, $b, $n))    echo "Yes";else    echo "No"; // This code is contributed// by Akanksha Rai(Abby_akku)
<script> // JavaScript program to check whether it is// possible or not to move from (0, 0) to (x, y)// in exactly n steps // Function to check whether it is possible// or not to move from (0, 0) to (x, y)// in exactly n stepsfunction Arrive(a, b, n){    if (n >= Math.abs(a) + Math.abs(b) &&       (n - (Math.abs(a) + Math.abs(b))) % 2 == 0)        return true;     return false;} // Driver codevar a = 5, b = 5, n = 11; if (Arrive(a, b, n))    document.write("Yes");else    document.write("No"); // This code is contributed by Ankita saini </script>
No
 
Shashank12
inderDuMCA
Akanksha_Rai
YatinGupta
ankita_saini
kothavvsaakash
Competitive Programming
Geometric
Mathematical
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Bits manipulation (Important tactics)
What is Competitive Programming and How to Prepare for It?
Bitwise Hacks for Competitive Programming
Algorithm Library | C++ Magicians STL Algorithm
Formatted output in Java
Program for distance between two points on earth
Closest Pair of Points using Divide and Conquer algorithm
How to check if a given point lies inside or outside a polygon?
How to check if two given line segments intersect?
Optimum location of point to minimize total distance