Two Balls Reachability Game | Java 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 ; Function returns if it 's possible to have X white and Y black balls or not. ; Finding gcd of ( x , y ) and ( a , b ) ; If gcd is same , it 's always  possible to reach (x, y) ; Here it 's never possible  if gcd is not same ; Driver code
import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void IsPossible ( int a , int b , int x , int y ) { int g = gcd ( x , y ) ; int initial = gcd ( a , b ) ; if ( initial == g ) { System . out . print ( "POSSIBLENEW_LINE"); } else { System . out . print ( "NOT POSSIBLENEW_LINE"); } } 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 ) ; } }