eolymp
bolt
Try our new interface for solving problems
Problems

Capracar conversion

Capracar conversion

The Indian mathematician D. R. Kaprekar is known for his works in number theory. One of his works is devoted to the so-called Capracar transformation. Consider the following operation. Let the number $x$ is given. Let $M$ is the largest number that can be obtained from $x$ by permuting its digits, and $m$ is the smallest number (this number can contain leading zeros). Let's designate as $K(x)$ the difference $M - m$, supplemented if necessary with leading zeros so that the number of digits in it is equal to the number of digits in $x$. For example $K(100) = 100 - 001 = 099, K(2414) = 4421 - 1244 = 3177$. Kaprekar proved that if we start with some four-digit number $x$, in which not all the digits are equal, and successively apply this operation to it (calculate $K(x), K(K(x)), ...)$, then sooner or later the number $6174$ will appear. For this number the equality $K(6174) = 7641 - 1467 = 6174$ is true, therefore, the process on it will be cycled. Your task is to write a program that calculates $K(x)$ by the number $x$. \InputFile One integer $x~(1 \le x \le 10^9)$ without leading zeroes. \OutputFile Print $K(x)$.
Time limit 1 second
Memory limit 128 MiB
Input example #1
100
Output example #1
099
Input example #2
2414
Output example #2
3177
Source 2009 Цикл интернет-олимпиад для школьников. Первая олимпиада, базовый уровень, September 19, Problem F