Coin Piles CSES Solution

Hello coders, Today we will see how to solve the ” Coin Piles CSES Solution “. The problems from CSES are a little bit trickier in comparison to HackerRank. So I suggest you, don’t hurry your self looking for a solution. Try to give extra time and write the approaches you can apply. And If you still didn’t find the solution CodeSagar has your back.

Let’s first understand the problem statement.

Problem statement:

You have two coin piles containing aa and bb coins. On each move, you can either remove one coin from the left pile and two coins from the right pile or two coins from the left pile and one coin from the right pile.

Your task is to efficiently find out if you can empty both the piles.


The first input line has an integer tt: the number of tests.

After this, there are tt lines, each of which has two integers aa and bb: the numbers of coins in the piles.


For each test, print “YES” if you can empty the piles and “NO” otherwise.


  • 1≤t≤1051≤t≤105
  • 0≤a,b≤1090≤a,b≤109


2 1
2 2
3 3


Coin Piles CSES Solution in java

//Coin Piles CSES Solution in java
import java.util.*;
import java.lang.Math.*;
public class CoinP {
    public static void main(String args[]) throws IOException{
		BufferedReader sc=new BufferedReader(new InputStreamReader(;
		int t=Integer.parseInt(sc.readLine());
            String input[]=(sc.readLine()).split(" ");
            int a=Integer.parseInt(input[0]);
			int b=Integer.parseInt(input[1]);
         System.out.println(((a + b) % 3 == 0 && (Math.min(a, b) * 2 >= Math.max(a, b)) ? "YES" : "NO"));
//Coin Piles CSES Solution in java


  1. Read the number of test cases t from input.
  2. Loop through each test case, reading the input values a and b.
  3. Calculate the sum (a + b) and check if it’s divisible by 3 using the modulo operator (%).
  4. Calculate the minimum and maximum of a and b using the Math.min and Math.max methods.
  5. Check if the double of the minimum value is greater than or equal to the maximum value.
  6. If steps 3 and 5 are true, print “YES”, otherwise print “NO”.
  7. Decrement t and repeat steps 2-6 until all test cases are processed.


The Coin Piles problem on CSES is to determine if it’s possible to divide a and b coins into two piles with the same number of coins by removing some coins from each pile. Each test case consists of two integers a and b.

The given code reads the input values for each test case and checks if it’s possible to divide the coins into two equal piles using the following conditions:

  1. The sum (a + b) must be divisible by 3.
  2. The minimum value of a and b multiplied by 2 must be greater than or equal to the maximum value of a and b.

If both conditions are true, then the coins can be divided into two equal piles, otherwise they cannot.

The code uses a while loop to process each test case and prints “YES” or “NO” accordingly.

Note that the code reads input using the BufferedReader class, which is more efficient than using Scanner for large input sizes.

Disclaimer: This problem(Coin Piles CSES Solution) is originally created by CSES. Codesagar only provides a solution for it. This solution is only for Educational and learning purposes.

Leave a Comment