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 = 0, Y = 0, K = 2Â
Output: YesÂ
Move 1: (0, 0) -> (0, 1)Â
Move 2: (0, 1) -> (0, 0)
Input: X = 5, Y = 8, K = 20Â
Output: NoÂ
Â
Â
Approach: It is clear that the shortest path to reach (X, Y) from (0, 0) will be minMoves = (|X| + |Y|). So, if K < minMoves then it is impossible to reach (X, Y) but if K ? minMoves then after reaching (X, Y) in minMoves number of moves the remaining (K – minMoves) number of moves have to be even in order to remain at that point for the rest of the moves.Â
So it is possible to reach (X, Y) from (0, 0) only if K ? (|X| + |Y|) and (K – (|X| + |Y|)) % 2 = 0.
Below is the implementation of the above approach:Â
Â
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; Â
// Function that returns true if it is // possible to move from (0, 0) to // (x, y) in exactly k moves bool isPossible( int x, int y, int k) {     // Minimum moves required     int minMoves = abs (x) + abs (y); Â
    // If possible     if (k >= minMoves && (k - minMoves) % 2 == 0)         return true ; Â
    return false ; } Â
// Driver code int main() { Â Â Â Â int x = 5, y = 8, k = 20; Â
    if (isPossible(x, y, k))         cout << "Yes" ;     else         cout << "No" ; Â
    return 0; } |
Java
// Java implementation of the approach class GFG {          // Function that returns true if it is     // possible to move from (0, 0) to     // (x, y) in exactly k moves     static boolean isPossible( int x, int y, int k)     {         // Minimum moves required         int minMoves = Math.abs(x) + Math.abs(y);              // If possible         if (k >= minMoves && (k - minMoves) % 2 == 0 )             return true ;              return false ;     }          // Driver code     public static void main (String[] args)     {         int x = 5 , y = 8 , k = 20 ;              if (isPossible(x, y, k))             System.out.println( "Yes" );         else             System.out.println( "No" );     } } Â
// This code is contributed by AnkitRai01 |
Python3
# Python3 implementation of the approach Â
# Function that returns true if it is # possible to move from (0, 0) to # (x, y) in exactly k moves def isPossible(x, y, k):          # Minimum moves required     minMoves = abs (x) + abs (y) Â
    # If possible     if (k > = minMoves and (k - minMoves) % 2 = = 0 ):         return True Â
    return False Â
# Driver code x = 5 y = 8 k = 20 Â
if (isPossible(x, y, k)): Â Â Â Â print ( "Yes" ) else : Â Â Â Â print ( "No" ) Â
# This code is contributed by Mohit Kumar |
C#
// C# implementation of the approach using System; class GFG {          // Function that returns true if it is     // possible to move from (0, 0) to     // (x, y) in exactly k moves     static bool isPossible( int x, int y, int k)     {         // Minimum moves required         int minMoves = Math.Abs(x) + Math.Abs(y);              // If possible         if (k >= minMoves && (k - minMoves) % 2 == 0)             return true ;              return false ;     }          // Driver code     public static void Main ()     {         int x = 5, y = 8, k = 20;              if (isPossible(x, y, k))             Console.Write( "Yes" );         else             Console.Write( "No" );     } } Â
// This code is contributed by Nidhi |
Javascript
<script> Â
// javascript implementation of the approach Â
     // Function that returns true if it is // possible to move from (0, 0) to // (x, y) in exactly k moves function isPossible(x , y , k) {     // Minimum moves required     var minMoves = Math.abs(x) + Math.abs(y); Â
    // If possible     if (k >= minMoves && (k - minMoves) % 2 == 0)         return true ; Â
    return false ; } Â
// Driver code Â
var x = 5, y = 8, k = 20; Â
if (isPossible(x, y, k))     document.write( "Yes" ); else     document.write( "No" ); Â
// This code contributed by shikhasingrajput Â
</script> |
No
Â
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!