Open In App

Check if two piles of coins can be emptied by repeatedly removing 2 coins from a pile and 1 coin from the other

Given two piles of coins consisting of N and M coins, the task is to check if both the piles can be emptied simultaneously by repeatedly removing 2 coins from one 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 = 2
Output: Yes
Explanation:
Remove 1 coin from 1st pile and 2 coins from the other pile. 
Therefore, the number of coins in both the piles is 0.
Input: N = 2, M = 2
Output: No
 

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

(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

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++ 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 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 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# 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




<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>

Time Complexity: O(1) // since no loop is used the algorithm takes constant time for all the operations
Auxiliary Space: O(1) // since no extra array is used the space taken by the algorithm is constant


Article Tags :