Open In App

Check if point (X, Y) can be reached from origin (0, 0) with jump of 1 and N perpendicularly simultaneously

Given a positive integer N and coordinates (X, Y), the task is to check if it is possible to reach (X, Y) from (0, 0) with the jump of 1 and N simultaneously in the perpendicular direction. If it is possible to reach (X, Y) then print Yes. Otherwise, print No.

Examples:



Input: N = 2, X = 5, Y = 4
Output: Yes
Explanation:
Following are the possible moves that leads to point (X, Y) from the current coordinate (0, 0):

  1. Performing the jump of (2, 1) from the current coordinate modifies it to (2, 1).
  2. Performing the jump of (2, 1) from the current coordinate modifies it to (4, 2).
  3. Performing the jump of (1, 2) from the current coordinate modifies it to (5, 4).

Input: N = 3, X = 5, Y = 4
Output: No



Approach: The given problem can be solved based on the observation that there are 4 possible ways to jump from any coordinate which are (1, N), (1, -N), (-1, N), and (-1, -N). Further, the problem can be divided into 3 different cases:

Therefore, the only case when it is not possible to reach (X, Y) from (0, 0) is when N is odd and X and Y have different parity i.e., X is even and Y is odd or vice versa.

Below is the implementation of the above approach:




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if (X, Y) is reachable
// from (0, 0) using the jumps of given type
string checkReachability(int N, int X, int Y)
{
    // Case where source & destination
    // are the same
    if (X == 0 && Y == 0) {
        return "YES";
    }
 
    // Check for even N (X, Y) is
    // reachable or not
    if (N % 2 == 0) {
        return "YES";
    }
 
    // If N is odd and parity of X and
    // Y is different return, no valid
    // sequence of jumps exist
    else {
        if (X % 2 != Y % 2) {
            return "NO";
        }
        else {
            return "YES";
        }
    }
}
 
// Driver Code
int main()
{
    int N = 2;
    int X = 5, Y = 4;
    cout << checkReachability(N, X, Y);
 
    return 0;
}




// Java code for the above approach
import java.io.*;
 
class GFG
{
   
  // Function to check if (X, Y) is reachable
// from (0, 0) using the jumps of given type
static String checkReachability(int N, int X, int Y)
{
   
    // Case where source & destination
    // are the same
    if (X == 0 && Y == 0) {
        return "YES";
    }
 
    // Check for even N (X, Y) is
    // reachable or not
    if (N % 2 == 0) {
        return "YES";
    }
 
    // If N is odd and parity of X and
    // Y is different return, no valid
    // sequence of jumps exist
    else {
        if (X % 2 != Y % 2) {
            return "NO";
        }
        else {
            return "YES";
        }
    }
}
 
// Driver Code
    public static void main (String[] args) {
      int N = 2;
    int X = 5, Y = 4;
    
        System.out.println(checkReachability(N, X, Y));
    }
}
 
// This code is contributed by Potta Lokesh




# Python 3 program for the above approach
 
# Function to check if (X, Y) is reachable
# from (0, 0) using the jumps of given type
def checkReachability(N, X, Y):
   
    # Case where source & destination
    # are the same
    if (X == 0 and Y == 0):
        return "YES"
 
    # Check for even N (X, Y) is
    # reachable or not
    if (N % 2 == 0):
        return "YES"
 
    # If N is odd and parity of X and
    # Y is different return, no valid
    # sequence of jumps exist
    else:
        if (X % 2 != Y % 2):
            return "NO"
        else:
            return "YES"
 
# Driver Code
if __name__ == '__main__':
    N = 2
    X = 5
    Y = 4
    print(checkReachability(N, X, Y))
     
    # This code is contributed by SURENDRA_GANGWAR.




// C# code for the above approach
using System;
 
class GFG
{
   
  // Function to check if (X, Y) is reachable
// from (0, 0) using the jumps of given type
static string checkReachability(int N, int X, int Y)
{
   
    // Case where source & destination
    // are the same
    if (X == 0 && Y == 0) {
        return "YES";
    }
 
    // Check for even N (X, Y) is
    // reachable or not
    if (N % 2 == 0) {
        return "YES";
    }
 
    // If N is odd and parity of X and
    // Y is different return, no valid
    // sequence of jumps exist
    else {
        if (X % 2 != Y % 2) {
            return "NO";
        }
        else {
            return "YES";
        }
    }
}
 
// Driver Code
    public static void Main (string[] args) {
      int N = 2;
      int X = 5, Y = 4;
    
    Console.WriteLine(checkReachability(N, X, Y));
    }
}
 
// This code is contributed by AnkThon




<script>
    // Function to check if (X, Y) is reachable
// from (0, 0) using the jumps of given type
string checkReachability(int N, int X, int Y)
{
 
    // Case where source & destination
    // are the same
    if (X == 0 && Y == 0) {
        return "YES";
    }
  
    // Check for even N (X, Y) is
    // reachable or not
    if (N % 2 == 0) {
        return "YES";
    }
  
    // If N is odd and parity of X and
    // Y is different return, no valid
    // sequence of jumps exist
    else {
        if (X % 2 != Y % 2) {
            return "NO";
        }
        else {
            return "YES";
        }
    }
}
    // Driver Code
 
    let N = 2, X = 5, Y = 4;
    document.write(checkReachability(N, X, Y));
 
// This code is contributed by dwivediyash.
</script>

Output: 
YES

 

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


Article Tags :