Check if players can meet on the same cell of the matrix in odd number of operations
Last Updated :
24 Feb, 2023
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++
#include <bits/stdc++.h>
using namespace std;
void Is_Possible_to_meet( int X, int Y, int A, int B)
{
X += Y - 2 + A;
A += B;
cout<< (X % 2 == 1 && A % 2 == 0 ? "NO" : "YES" ) << endl;
}
int main() {
int X = 2, Y = 2;
int A = 2, B = 1;
Is_Possible_to_meet(X, Y, A, B);
}
|
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
int X = 2 , Y = 2 ;
int A = 2 , B = 1 ;
Is_Possible_to_meet(X, Y, A, B);
}
static void Is_Possible_to_meet( int X, int Y, int A,
int B)
{
X += Y - 2 + A;
A += B;
System.out.println(
X % 2 == 1 && A % 2 == 0 ? "NO" : "YES" );
}
}
|
C#
using System;
public class GFG{
static void Is_Possible_to_meet( int X, int Y, int A,
int B)
{
X += Y - 2 + A;
A += B;
Console.Write(
X % 2 == 1 && A % 2 == 0 ? "NO" : "YES" );
}
static public void Main (){
int X = 2, Y = 2;
int A = 2, B = 1;
Is_Possible_to_meet(X, Y, A, B);
}
}
|
Javascript
function Is_Possible_to_meet( X, Y, A, B)
{
X += Y - 2 + A;
A += B;
console.log(X % 2 == 1 && A % 2 == 0 ? "NO" : "YES" );
}
let X = 2, Y = 2;
let A = 2, B = 1;
Is_Possible_to_meet(X, Y, A, B);
|
Python3
def Is_Possible_to_meet(X, Y, A, B):
X + = Y - 2 + A
A + = B
print ( "NO" if X % 2 = = 1 and A % 2 = = 0 else "YES" )
if __name__ = = '__main__' :
X, Y = 2 , 2
A, B = 2 , 1
Is_Possible_to_meet(X, Y, A, B)
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...