Largest number with sum S and N number of digits Given a number N and sum as S .What is the mathematical approach to construct the largest number  having N digits whose sum is S and smallest number of N digits  whose sum is again S. Leading zero digit numbers are not considered.
 A: (a)Fill the first $\lfloor\frac S9 \rfloor$ digits by $9$Fill the next digit by $S\mod9$ Fill the rest of the digits by $0$
(b)Fill the last $\lfloor\frac {S-1}9 \rfloor$ digits by $9$Fill the previous digit by $S-1\mod9$  Fill the rest of the digits by $0$ Add $1$ to the first digit.
Example from the comment below, 
$S=15$, $N=2$
$\lfloor\frac {S-1}9 \rfloor = 1$
$S-1\mod9 = 5$
So, you have $59$. No remaining digits, so no $0$s. Now, the last step - add $1$ gives $69$
A: I assume you mean a number with $N$ digits in decimal base and having the digit sum $S$.
It runs down to partition the number $S$ into $N$ parts (if possible at all!), where the parts are in the range $\{ 0, \dotsc, 9 \}$ and sorting it such that either the largest parts come first for a maximal number, or such that the smallest parts come first for a minimal number. Of all feasible partitions one picks the one which comes first under the considered order.
One could pose it as integer linear programming (ILP) problem, e.g.
\begin{matrix}
\max & c^\top x \\
\text{w.r.t.} & Ax = b \\
& 0 \le x \\
& x \le 9
\end{matrix}
with
\begin{align}
c^\top &= (10^{N-1}, 10^{N-2}, \dotsc, 1) \in \mathbb{R}^{1 \times N} \\
A &= (1, \dotsc, 1) \in \mathbb{R}^{1 \times N} \\
x &= (x_!, \dotsc, x_N)^\top \in \mathbb{Z}^N \\
b &= (S) \in \mathbb{N}
\end{align}
and use a solver.
The minimal number is a bit trickier, because of the convention that leading $0$ digits are dropped (except for single digit numbers).
