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.
Input
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.
Output
For each test, print “YES” if you can empty the piles and “NO” otherwise.
Constraints
- 1≤t≤1051≤t≤105
- 0≤a,b≤1090≤a,b≤109
Example
Input:3
2 1
2 2
3 3
Output:YES
NO
YES
Coin Piles CSES Solution in java
//Coin Piles CSES Solution in java import java.util.*; import java.lang.Math.*; import java.io.*; public class CoinP { public static void main(String args[]) throws IOException{ BufferedReader sc=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(sc.readLine()); while(t>0){ 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")); t--; } } } //Coin Piles CSES Solution in java
Algorithm:
- Read the number of test cases t from input.
- Loop through each test case, reading the input values a and b.
- Calculate the sum (a + b) and check if it’s divisible by 3 using the modulo operator (%).
- Calculate the minimum and maximum of a and b using the Math.min and Math.max methods.
- Check if the double of the minimum value is greater than or equal to the maximum value.
- If steps 3 and 5 are true, print “YES”, otherwise print “NO”.
- Decrement t and repeat steps 2-6 until all test cases are processed.
Explanation:
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:
- The sum (a + b) must be divisible by 3.
- 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.