Largest number made up of X and Y with count of X divisible by Y and of Y by X | 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
function largestNumber ( n , X , Y ) { let maxm = Math . max ( X , Y ) ; Y = X + Y - maxm ; X = maxm ; let Xs = 0 ; let Ys = 0 ; while ( n > 0 ) { if ( n % Y == 0 ) { Xs += n ; n = 0 ; } else { n -= X ; Ys += X ; } } if ( n == 0 ) { while ( Xs -- > 0 ) document . write ( X ) ; while ( Ys -- > 0 ) document . write ( Y ) ; } else document . write ( " " ) ; } let n = 19 , X = 7 , Y = 5 ; largestNumber ( n , X , Y ) ;