Check if players can meet on the same cell of the matrix in odd number of operations
Given four integers X, Y, A, and B. X and Y denotes the rows and the columns of a matrix such that (X, Y ≥ 2). Check if both the players can meet on the same cell of the matrix in an exactly odd number of operations by following the given conditions:
- Player 1 is at the top left corner of the matrix initially.
- Player 2 is at the bottom right of the matrix initially.
- In one operation, Player 1 can make exactly A steps, or Player 2 can make exactly B steps. In one step a player can move only left, right, up, or down cell from the current cell.
Examples:
Input: X = 2, Y = 2, A = 2, B = 1
Output: YES
Explanation: Operations are as follows:
- Operation 1: Player 1 can make exactly 2 steps and initially at the top left corner(1, 1). The sequence of two steps is as: (1, 1) -> (1, 2) -> (2, 2). Player 2 is initially present at the bottom right corner(N.M) = (2, 2). It can be verified that both players are present at (2, 2) now. The number of operations is 1, Which is exactly odd. Therefore, the output is YES.
Input: X = 2, Y = 2, A = 1, B = 2
Output: YES
Explanation: Operations are as follows:
- Operation 1: Player 1 moves to the right cell from the current position, Formally (1, 1) -> (1, 2).
- Operation 2: Player 2, First moves to the left cell from the current position(2, 2) and then right side to one step, (2, 2) -> (2, 1) -> (2, 2). Formally, returns to its initial position.
- Operation 3: Player 1 is at currently (1, 2), Makes one step downwards from (1, 2) to (2, 2). It can be verified that now both the player are at (2, 2) in 3 operations, Which is odd. Therefore, the output is YES.
Input: X = 2, Y = 2, A = 1, B = 1
Output: NO
Explanation: It can be verified that it is not possible to meet on the same cell in an exactly odd number of operations. Hence the output is NO.
Approach: Implement the idea below to solve the problem:
The problem is observation based and can be solved by using Mathematical concepts.
Steps were taken to solve the problem:
- Increment X by Y – 2 + A, Formally X+=Y-2+A.
- Increment A by B, Formally A += B.
- If (X % 2 == 1 && A % 2 == 0) then output YES else NO.
Below is the code to implement the above approach:
C++
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // Method for output the answer void Is_Possible_to_meet( int X, int Y, int A, int B) { // Implementing idea for output the answer X += Y - 2 + A; A += B; cout<< (X % 2 == 1 && A % 2 == 0 ? "NO" : "YES" ) << endl; } int main() { // Input value of X and Y int X = 2, Y = 2; // Input value of A and B int A = 2, B = 1; // Function call Is_Possible_to_meet(X, Y, A, B); } |
Java
// Java code to implement the approach import java.util.*; class GFG { // Driver Function public static void main(String[] args) { // Input value of X and Y int X = 2 , Y = 2 ; // Input value of A and B int A = 2 , B = 1 ; // Function call Is_Possible_to_meet(X, Y, A, B); } // Method for output the answer static void Is_Possible_to_meet( int X, int Y, int A, int B) { // Implementing idea for output // the answer X += Y - 2 + A; A += B; System.out.println( X % 2 == 1 && A % 2 == 0 ? "NO" : "YES" ); } } |
C#
// C# code to implement the approach using System; public class GFG{ // Method for output the answer static void Is_Possible_to_meet( int X, int Y, int A, int B) { // Implementing idea for output // the answer X += Y - 2 + A; A += B; Console.Write( X % 2 == 1 && A % 2 == 0 ? "NO" : "YES" ); } // Driver Function static public void Main (){ // Input value of X and Y int X = 2, Y = 2; // Input value of A and B int A = 2, B = 1; // Function call Is_Possible_to_meet(X, Y, A, B); } } |
Javascript
// JS code to implement the approach // Method for output the answer function Is_Possible_to_meet( X, Y, A, B) { // Implementing idea for output the answer X += Y - 2 + A; A += B; console.log(X % 2 == 1 && A % 2 == 0 ? "NO" : "YES" ); } // Input value of X and Y let X = 2, Y = 2; // Input value of A and B let A = 2, B = 1; // Function call Is_Possible_to_meet(X, Y, A, B); // this code is contributed by poojaagarwal2. |
Python3
def Is_Possible_to_meet(X, Y, A, B): # Implementing idea for output the answer X + = Y - 2 + A A + = B print ( "NO" if X % 2 = = 1 and A % 2 = = 0 else "YES" ) # Main function if __name__ = = '__main__' : # Input value of X and Y X, Y = 2 , 2 # Input value of A and B A, B = 2 , 1 # Function call Is_Possible_to_meet(X, Y, A, B) |
YES
Time Complexity: O(1)
Auxiliary Space: O(1)
Please Login to comment...