Largest number made up of X and Y with count of X divisible by Y and of Y by X | Java program to implement the above approach ; Function to generate and return the largest number ; Store the smaller in Y ; Store the larger in X ; Stores respective counts ; If N is divisible by Y ; Append X , N times to the answer ; Reduce N to zero ; Reduce N by X ; Append Y , X times to the answer ; If number can be formed ; Otherwise ; Driver code
import java . util . * ; class GFG { public static void largestNumber ( int n , int X , int Y ) { int maxm = Math . max ( X , Y ) ; Y = X + Y - maxm ; X = maxm ; int Xs = 0 ; int Ys = 0 ; while ( n > 0 ) { if ( n % Y == 0 ) { Xs += n ; n = 0 ; } else { n -= X ; Ys += X ; } } if ( n == 0 ) { while ( Xs -- > 0 ) System . out . print ( X ) ; while ( Ys -- > 0 ) System . out . print ( Y ) ; } else System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int n = 19 , X = 7 , Y = 5 ; largestNumber ( n , X , Y ) ; } }