Given two piles of coins consisting of** N** and **M** number of coins, the task is to check if both the piles can be emptied simultaneously by repeatedly removing **2 coins** from a pile and **1 coin** from the other pile. If both the piles can be made empty, then print **“Yes”**. Otherwise, print **“No”**.

**Examples:**

Input:N = 1, M = 2Output:YesExplanation:

Remove 1 coin from 1^{st}pile and 2 coins from the other pile. Therefore, the number of coins in both the piles is 0.

Input:N = 2, M = 2Output:No

**Approach:** The given problem can be solved based on the following observations:

- There are two ways to remove coins from piles, i.e. either remove
**2 coins**from**pile 1**and**1 coin**from**pile 2**or**1 coin**from**pile 1**and**2 coins**from**pile 2**. - Let
**X**be the number of moves made for the first removal and**Y**be the number of moves made for the second removal, then the remaining number of coins in the two piles are**(N – 2*X – Y)**and**(M – 2*Y – X)**respectively. - After performing
**(X + Y)**moves, the piles must be emptied i.e.,

(N – 2*X – Y) = 0 and (M – 2*Y – X) = 0

Rearranging terms in the above equations generates the equations:

N = 2*X + Y and M = 2*Y + X

- The total number of coins in both piles is
**(N + M)**=**(2*X + Y) + (X + 2*Y) = 3(X + Y)**. - Therefore, to make both the piles empty, below are the conditions:
- The sum of the coins in both piles should be a multiple of 3.
- The maximum of
**N**and**M**must less than twice the minimum of**N**and**M**as one whole pile will become empty and some coins will still be left in another one. Therefore, the size of the larger pile should not be more than twice the size of the smaller pile.

Therefore, the idea is to check if the sum of total number of coins is a multiple of **3** and the maximum number of coins is at most twice the minimum number of coins, then print **“Yes”**. Otherwise, print **“No”**.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to check if two given piles` `// can be emptied by repeatedly removing` `// 2 coins from a pile and 1 coin from the other` `void` `canBeEmptied(` `int` `A, ` `int` `B)` `{` ` ` `// If maximum of A & B exceeds` ` ` `// the twice of minimum of A & B` ` ` `if` `(max(A, B) > 2 * min(A, B)) {` ` ` `// Not possible to` ` ` `// empty the piles` ` ` `cout << ` `"No"` `;` ` ` `return` `;` ` ` `}` ` ` `// If sum of both the coins is` ` ` `// divisible by 3, then print Yes` ` ` `if` `((A + B) % 3 == 0)` ` ` `cout << ` `"Yes"` `;` ` ` `// Otherwise, print "No"` ` ` `else` ` ` `cout << ` `"No"` `;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `A = 1, B = 2;` ` ` `canBeEmptied(A, B);` ` ` `return` `0;` `}` |

## Java

`// Java program for above approach` `import` `java.util.*;` `class` `GFG{` `// Function to check if two given piles` `// can be emptied by repeatedly removing` `// 2 coins from a pile and 1 coin from the other` `static` `void` `canBeEmptied(` `int` `A, ` `int` `B)` `{` ` ` ` ` `// If maximum of A & B exceeds` ` ` `// the twice of minimum of A & B` ` ` `if` `(Math.max(A, B) > ` `2` `* Math.min(A, B))` ` ` `{` ` ` ` ` `// Not possible to` ` ` `// empty the piles` ` ` `System.out.println(` `"No"` `);` ` ` `return` `;` ` ` `}` ` ` `// If sum of both the coins is` ` ` `// divisible by 3, then print Yes` ` ` `if` `((A + B) % ` `3` `== ` `0` `)` ` ` `System.out.println(` `"Yes"` `);` ` ` `// Otherwise, print "No"` ` ` `else` ` ` `System.out.println(` `"No"` `);` `}` `// Driver Code` `public` `static` `void` `main (String[] args)` `{` ` ` `int` `A = ` `1` `, B = ` `2` `;` ` ` ` ` `canBeEmptied(A, B);` `}` `}` `// This code is contributed by sanjoy_62` |

## Python3

`# Python3 program for the above approach` `# Function to check if two given piles` `# can be emptied by repeatedly removing` `# 2 coins from a pile and 1 coin from the other` `def` `canBeEmptied(A, B):` ` ` ` ` `# If maximum of A & B exceeds` ` ` `# the twice of minimum of A & B` ` ` `if` `(` `max` `(A, B) > ` `2` `*` `min` `(A, B)):` ` ` ` ` `# Not possible to` ` ` `# empty the piles` ` ` `print` `(` `"No"` `)` ` ` `return` ` ` `# If sum of both the coins is` ` ` `# divisible by 3, then print Yes` ` ` `if` `((A ` `+` `B) ` `%` `3` `=` `=` `0` `):` ` ` `print` `(` `"Yes"` `)` ` ` `# Otherwise, print "No"` ` ` `else` `:` ` ` `print` `(` `"No"` `)` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `A ` `=` `1` ` ` `B ` `=` `2` ` ` `canBeEmptied(A, B)` ` ` `# This code is contributed by bgangwar59.` |

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG{` `// Function to check if two given piles` `// can be emptied by repeatedly removing` `// 2 coins from a pile and 1 coin from the other` `static` `void` `canBeEmptied(` `int` `A, ` `int` `B)` `{` ` ` ` ` `// If maximum of A & B exceeds` ` ` `// the twice of minimum of A & B` ` ` `if` `(Math.Max(A, B) > 2 * Math.Min(A, B))` ` ` `{` ` ` ` ` `// Not possible to` ` ` `// empty the piles` ` ` `Console.WriteLine(` `"No"` `);` ` ` `return` `;` ` ` `}` ` ` `// If sum of both the coins is` ` ` `// divisible by 3, then print Yes` ` ` `if` `((A + B) % 3 == 0)` ` ` `Console.WriteLine(` `"Yes"` `);` ` ` `// Otherwise, print "No"` ` ` `else` ` ` `Console.WriteLine(` `"No"` `);` `}` `// Driver Code` `public` `static` `void` `Main ()` `{` ` ` `int` `A = 1, B = 2;` ` ` ` ` `canBeEmptied(A, B);` `}` `}` `// This code is contributed by mohit kumar 29` |

## Javascript

`<script>` `// Javascript program for the above approach` `// Function to check if two given piles` `// can be emptied by repeatedly removing` `// 2 coins from a pile and 1 coin from the other` `function` `canBeEmptied(A, B)` `{` ` ` ` ` `// If maximum of A & B exceeds` ` ` `// the twice of minimum of A & B` ` ` `if` `(Math.max(A, B) > 2 * Math.min(A, B))` ` ` `{` ` ` `// Not possible to` ` ` `// empty the piles` ` ` `document.write(` `"No"` `);` ` ` `return` `;` ` ` `}` ` ` `// If sum of both the coins is` ` ` `// divisible by 3, then print Yes` ` ` `if` `((A + B) % 3 == 0)` ` ` `document.write(` `"Yes"` `);` ` ` `// Otherwise, print "No"` ` ` `else` ` ` `document.write(` `"No"` `);` `}` `// Driver Code` `let A = 1, B = 2;` `canBeEmptied(A, B);` `// This code is contributed by subhammahato348` `</script>` |

**Output:**

Yes

**Time Complexity:** O(1)**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**