Find the Largest number with given number of digits and sum of digits | Prints the smalles possible number with digit sum ' s ' and ' m ' number of digits . ; If sum of digits is 0 , then a number is possible only if number of digits is 1. ; Sum greater than the maximum possible sum . ; Create an array to store digits of result ; Fill from most significant digit to least significant digit . ; Fill 9 first to make the number largest ; If remaining sum becomes less than 9 , then fill the remaining sum ; Driver code
function findLargest ( m , s ) { if ( s == 0 ) { ( m == 1 ) ? document . write ( " " + 0 ) : document . write ( " " ) ; return ; } if ( s > 9 * m ) { document . write ( " " ) ; return ; } let res = new Array ( m ) ; for ( let i = 0 ; i < m ; i ++ ) { if ( s >= 9 ) { res [ i ] = 9 ; s -= 9 ; } else { res [ i ] = s ; s = 0 ; } } document . write ( " " ) ; for ( let i = 0 ; i < m ; i ++ ) document . write ( res [ i ] ) ; } let s = 9 , m = 2 ; findLargest ( m , s ) ;