How to get the maximum and minimum number of length $m$ and the sum of the digits $s$

451 Views Asked by At

How to get the maximum and minimum of length $m$ and the sum of the digits $s$

By example:

Length: 2 Sum of its digits: 15

Max: 96, Min: 69

Length: 2 Sum of its digits: 2

Max: 20, Min: 11

1

There are 1 best solutions below

1
On

The basic answer is for the maximum you need all the big numbers up front and for the minimum you need all the little numbers up front. For the max, fill from the left with nines as far as they go, the remainder (if any) in the next digit, and finish with zeros. For the min, fill from the right with nines, then the remainder, then zeros, saving a $1$ for the lead digit.