Two Balls Reachability Game


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.

Examples:

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

Approach:



We have to solve this problem using the property of gcd. Let’s see how.

  1. 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.
  2. 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)
    
  3. 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:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to Find is it possible
// to have X white and Y black
// balls at the end.
#include <bits/stdc++.h>
using namespace std;
  
// Recursive function to return 
// gcd of a and b
int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
  
// Function returns if it's  
// possible to have X white 
// and Y black balls or not.
void IsPossible(int a, int b, 
                int x, int y)
{
  
    // Finding gcd of (x, y) 
    // and (a, b)
    int final = gcd(x, y);
    int initial = gcd(a, b);
  
     // If gcd is same, it's always
    // possible to reach (x, y)
    if (initial == final) 
    {
         
        cout << "POSSIBLE\n";
    }
    else
    {
        // Here it's never possible
        // if gcd is not same
        cout << "NOT POSSIBLE\n";
    }
}
  
// Driver Code
int main()
{
  
    int A = 1, B = 2, X = 4, Y = 11;
    IsPossible(A, B, X, Y);
  
    A = 2, B = 2, X = 3, Y = 6;
    IsPossible(A, B, X, Y);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to Find is it possible
// to have X white and Y black
// balls at the end.
import java.io.*;
  
class GFG{
  
// Recursive function to return 
// gcd of a and b
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
  
// Function returns if it's 
// possible to have X white 
// and Y black balls or not.
static void IsPossible(int a, int b, 
                       int x, int y)
{
  
    // Finding gcd of (x, y) 
    // and (a, b)
    int g = gcd(x, y);
    int initial = gcd(a, b);
      
    // If gcd is same, it's always
    // possible to reach (x, y)
    if (initial == g) 
    {
        System.out.print("POSSIBLE\n");
    }
    else
    {
        // Here it's never possible
        // if gcd is not same
        System.out.print("NOT POSSIBLE\n");
    }
}
  
// Driver code
public static void main(String args[])
{
    int A = 1, B = 2, X = 4, Y = 11;
    IsPossible(A, B, X, Y);
  
    A = 2; B = 2; X = 3; Y = 6;
    IsPossible(A, B, X, Y);
}
}
  
// This code is contributed by shivanisinghss2110

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find is it possible 
# to have X white and Y black 
# balls at the end. 
  
# Recursive function to return 
# gcd of a and b 
def gcd(a, b) : 
  
    if (b == 0) : 
        return a; 
    return gcd(b, a % b); 
  
  
# Function returns if it's 
# possible to have X white 
# and Y black balls or not. 
def IsPossible(a, b, x, y) : 
  
    # Finding gcd of (x, y) 
    # and (a, b) 
    final = gcd(x, y); 
    initial = gcd(a, b); 
  
    # If gcd is same, it's always 
    # possible to reach (x, y) 
    if (initial == final) :
        print("POSSIBLE"); 
      
    else :
          
        # Here it's never possible 
        # if gcd is not same 
        print("NOT POSSIBLE"); 
  
  
# Driver Code 
if __name__ == "__main__" :
      
    A = 1; B = 2; X = 4; Y = 11;
    IsPossible(A, B, X, Y);
      
    A = 2; B = 2; X = 3; Y = 6;
    IsPossible(A, B, X, Y); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to Find is it possible
// to have X white and Y black
// balls at the end.
using System; 
using System.Linq;
  
class GFG { 
      
// Recursive function to return 
// gcd of a and b
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
  
    return gcd(b, a % b);
}
  
// Function returns if it's 
// possible to have X white 
// and Y black balls or not.
static void IsPossible(int a, int b, 
                       int x, int y)
{
      
    // Finding gcd of (x, y) 
    // and (a, b)
    int g = gcd(x, y);
    int initial = gcd(a, b);
      
    // If gcd is same, it's always
    // possible to reach (x, y)
    if (initial == g) 
    {
        Console.Write("POSSIBLE\n");
    }
    else
    {
          
        // Here it's never possible
        // if gcd is not same
        Console.Write("NOT POSSIBLE\n");
    }
}
  
// Driver code
static public void Main()
{
    int A = 1, B = 2;
    int X = 4, Y = 11;
    IsPossible(A, B, X, Y);
  
    A = 2; B = 2; 
    X = 3; Y = 6;
    IsPossible(A, B, X, Y);
}
}
  
// This code is contributed by shivanisinghss2110

chevron_right


Output:

POSSIBLE
NOT POSSIBLE

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.