Open In App

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

Last Updated : 06 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • Case 1 – where N is even: In such cases any coordinate (X, Y) is reachable. Let’s consider X and Y one at a time. For a coordinate X, the sequence of (1, N), (1, -N), (1, N), (1, -N), … jumps can be followed. The resulting coordinate will be either (X, 0) which is the desired coordinate, or (X, N). Since N is even, the sequence of jumps (N, -1), (-N, -1), (N, -1), (N, -1), … can also be followed to reach (X, 0). Similarly, (0, Y) can be reached, and combining the sequence of jumps to reach (X, 0) and (0, Y), (X, Y) can be reached.
  • Case 2 – where N is odd and both X and Y are either even or odd: These cases can be handled by following a sequence of operations similar to Case 1.
  • Case 3 – where N is odd and X and Y have different parity: In such cases, there is no possible sequence of jumps to reach (X, Y) because according to Case 2, (X + 1, Y) and (X, Y + 1) must be reachable as they both will have the same parity and there is no possible sequence of operations to cover a distance of (1, 0) or (0, 1).

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++




// 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




// 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


Python3




# 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#




// 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


Javascript




<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)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads