Given A numbers of white and B numbers of black balls. You need to have X number of white and Y number of black balls (A <= X, B <= Y) to win the game by doing some(zero or more) operations.
In one operation: At any moment if you have p white and q black balls then at that moment you can buy q white or p black balls.
Find if it’s possible to have X white and Y black balls at the end or not.
Input: A = 1, B = 1, X = 3, Y = 8 Output: POSSIBLE Explanation: The steps are, (1, 1)->(1, 2)->(3, 2)->(3, 5)->(3, 8) Input: A = 3, Y = 2, X = 4, Y = 6 Output: NOT POSSIBLE
We have to solve this problem using the property of gcd. Let’s see how.
- Initially, we have A white and B black ball. We have to get rest of the X-A Red balls and Y-B Black balls.
Below is the property of gcd of two numbers that we will use,
gcd(x, y) = gcd(x + y, y) gcd(x, y) = gcd(x, y + x)
- This property is the same as the operation which is mentioned in the question. So from here, we get that if the gcd of the final state is the same as gcd of initial state then it is always possible to reach the goal, otherwise not.
Below is the implementation of the above approach:
POSSIBLE NOT POSSIBLE
- Find ways to arrange K green balls among N balls such that exactly i moves is needed to collect all K green balls
- Ways to arrange Balls such that adjacent balls are of different types
- Game Theory (Normal form game) | Set 2 (Game with Pure Strategy)
- Game Theory (Normal-form game) | Set 3 (Game with Mixed Strategy)
- Game Theory (Normal-form Game) | Set 6 (Graphical Method [2 X N] Game)
- Game Theory (Normal-form Game) | Set 7 (Graphical Method [M X 2] Game)
- Determine winner of the Game by arranging balls in a row
- Combinatorial Game Theory | Set 2 (Game of Nim)
- Game Theory (Normal-form Game) | Set 4 (Dominance Property-Pure Strategy)
- Game Theory (Normal-form Game) | Set 5 (Dominance Property-Mixed Strategy)
- Program to check if water tank overflows when n solid balls are dipped in the water tank
- Ways to choose balls such that at least one ball is chosen
- Find total no of collisions taking place between the balls in which initial direction of each ball is given
- Coin game of two corners (Greedy Approach)
- Two player game in which a player can remove all occurrences of a number
- Optimal Strategy for a Game | DP-31
- Combinatorial Game Theory | Set 4 (Sprague - Grundy Theorem)
- Minimum Players required to win the game
- Predict the winner of the game on the basis of absolute difference of sum by selecting numbers
- Minimum possible final health of the last monster in a game