Last Updated: 31 Oct, 2022

Odd Even

Easy
Asked in company
TCS

Problem statement

Alice has two numbers, 'X' and 'Y'. He is asked to perform the following operations any number of times.

Alice adds 1 to 'X' if 'X' is odd. Alice adds 2 to 'X' if 'X' is even. Will he be able to make 'X' equal to 'Y'?


Return 1 if 'X' can be made equal to 'Y' using the above operations any number of times(possibly 0) and 0 otherwise.

Note: Assume 1-based indexing.


For example:
Let 'X' = 3 and 'Y' = 6, Alice adds 1 to 'X' since 'X' is odd. Now 'X' will become 4. Then, Alice will add 2 to 'X', since 'X' is even, and it will become 6. 'X' becomes equal to 'Y'. Hence we will return 1 as the answer.
Input Format:-
The first line contains an integer 'T', which denotes the number of test cases.
For every test case:-
The first line contains two integers, 'X' and 'Y', the variables mentioned above.
Output Format:-
For each test case, Return 1 if 'X' can be made equal to 'Y' using the above operations any number of times(possibly 0) and 0 otherwise.
Note:-
You don’t need to print anything. Just implement the given function.
Constraints:-
1 <= 'T' <= 10
1 <= 'X', 'Y' <= 10^9
Time Limit: 1 sec

Approaches

01 Approach

Approach:-

 

If 'x' > 'y', 'x' can never be made equal to 'y'.

If 'y' is even, we can always make 'x' equal to 'y' by performing some number of operations. 

If 'y' is odd, Let's take two cases:

  • If 'x' is even, we will always get an even number, As we always add 2 to 'x'.
  • If 'x' is odd, then after 1 operation, 'x' will be even, as we are adding 1 to 'x'. After this, 'x' will always be even after any number of operations.

So, when 'y' is odd, it is impossible to make 'x' equal to 'y' except if 'x' is already equal to 'y'.

           

 Algorithm:-

 

  • Check if 'x' and 'y' are already equal.
    • Return 1.
  • Check if 'x' is greater than 'y'.
    • Return 0.
  • Check if 'y' is odd.
    • Return 0.
  • Else
    • Return 1.