# Check if X and Y can be made zero by using given operation any number of times

Given two integers **X** and **Y**, the task is to check if these two integers can be made equal to 0 by using the given operation any number of times. An operation is described as follows –

- Choose an arbitrary integer
**Z**. - Update the values with either of the following values:
- X = X – 2*Z and Y = Y – 3*Z
- X = X – 3*Z and Y = Y – 2*Z

**Examples:**

Input:X = 6, Y = 9

Output:Yes

Explanation:

Operation 1: Choose Z = 3, X = 6 – 2*3 = 0 and Y = 9 – 3*3 = 0

Since X and Y can be made equal to 0 using 1 operation, the required answer is Yes.

Input:X = 33, Y = 27

Output:Yes

Explanation:

Operation 1: Choose Z = 9, X := 33 – 3*9 = 6 and Y := 27 – 2*9 = 9

Operation 2: Choose Z = 3, X := 6 – 2*3 = 0 and Y := 9 – 3*3 = 0

Since X and Y can be made equal to 0 using 2 operation, the required answer is Yes.

**Approach:** Let’s assume X ≤ Y. Then the answer is Yes if two following conditions holds:

**(X + Y) mod 5 = 0**: because after each operation the value (X + Y) mod 5 does not change.

Let’s assume some arbitrary number**Z**has been chosen.

Therefore, the value**(X + Y)**will be changed to((X - 3Z) + (Y - 2Z))

This is equal to

(X + Y - 5Z)

For this value to be equal to 0,

**X + Y = 5Z**. Therefore, on taking mod on both the sides, (X + Y) mod 5 has to be equal to 0.**3*X >= 2*Y**so that the subtraction doesnt make the values of X and Y negative.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check if X and Y ` `// can be made equal to zero by ` `// using given operation any number of times ` `void` `ifPossible(` `int` `X, ` `int` `Y) ` `{ ` ` ` `if` `(X > Y) ` ` ` `swap(X, Y); ` ` ` ` ` `// Check for the two conditions ` ` ` `if` `((X + Y) % 5 == 0 and 3 * X >= 2 * Y) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `X = 33, Y = 27; ` ` ` ` ` `ifPossible(X, Y); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to check if X and Y ` `// can be made equal to zero by ` `// using given operation any number of times ` `static` `void` `ifPossible(` `int` `X, ` `int` `Y) ` `{ ` ` ` `if` `(X > Y) ` ` ` `swap(X, Y); ` ` ` ` ` `// Check for the two conditions ` ` ` `if` `((X + Y) % ` `5` `== ` `0` `&& ` `3` `* X >= ` `2` `* Y) ` ` ` `System.out.print(` `"Yes"` `); ` ` ` `else` ` ` `System.out.print(` `"No"` `); ` `} ` `static` `void` `swap(` `int` `x, ` `int` `y) ` `{ ` ` ` `int` `temp = x; ` ` ` `x = y; ` ` ` `y = temp; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `X = ` `33` `, Y = ` `27` `; ` ` ` ` ` `ifPossible(X, Y); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to check if X and Y ` `# can be made equal to zero by ` `# using given operation any number of times ` `def` `ifPossible(X, Y): ` ` ` `if` `(X > Y): ` ` ` `X, Y ` `=` `Y, X ` ` ` ` ` `# Check for the two conditions ` ` ` `if` `((X ` `+` `Y) ` `%` `5` `=` `=` `0` `and` `3` `*` `X >` `=` `2` `*` `Y): ` ` ` `print` `(` `"Yes"` `) ` ` ` `else` `: ` ` ` `print` `(` `"No"` `) ` ` ` `# Driver code ` `X ` `=` `33` `Y ` `=` `27` ` ` `ifPossible(X, Y) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to check if X and Y ` `// can be made equal to zero by ` `// using given operation any number of times ` `static` `void` `ifPossible(` `int` `X, ` `int` `Y) ` `{ ` ` ` `if` `(X > Y) ` ` ` `swap(X, Y); ` ` ` ` ` `// Check for the two conditions ` ` ` `if` `((X + Y) % 5 == 0 && 3 * X >= 2 * Y) ` ` ` `Console.WriteLine(` `"Yes"` `); ` ` ` `else` ` ` `Console.WriteLine(` `"No"` `); ` `} ` `static` `void` `swap(` `int` `x, ` `int` `y) ` `{ ` ` ` `int` `temp = x; ` ` ` `x = y; ` ` ` `y = temp; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `X = 33, Y = 27; ` ` ` ` ` `ifPossible(X, Y); ` `} ` `} ` ` ` `// This code is contributed by Yash_R ` |

*chevron_right*

*filter_none*

**Output:**

Yes

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Check if given array can be made 0 with given operations performed any number of times
- Check if the given array can be reduced to zeros with the given operation performed given number of times
- Queries to check whether all the elements can be made positive by flipping signs exactly K times
- Check whether the number can be made palindromic after adding K
- Check whether the number can be made perfect square after adding 1
- Check whether the number can be made perfect square after adding K
- Check if N and M can be made equal by increasing N by A and decreasing M by B
- Check if all elements of the given array can be made 0 by decrementing value in pairs
- Check if given intervals can be made non-overlapping by adding/subtracting some X
- Check whether an Array can be made 0 by splitting and merging repeatedly
- Check if it is possible to make x and y zero at same time with given operation
- Check if elements of array can be made equal by multiplying given prime numbers
- Check if two arrays are permutations of each other using Mathematical Operation
- Nth number made up of odd digits only
- Largest number divisible by 90 that can be made using 0 and 5
- Find the n-th number made of even digits only
- Total number of different staircase that can made from N boxes
- Finding n-th number made of prime digits (2, 3, 5 and 7) only
- Largest number made up of X and Y with count of X divisible by Y and of Y by X
- Find position of the given number among the numbers made of 4 and 7

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.