Kaprekar Transformation
Time limit2sMemory limit1024 MB
Given x, the task is to rearrange its digits into the largest and smallest numbers and print their difference, padded to the digit count of x.
- Level
Easy2 of 10
- Topics
- Sorting, Implementation
- Solved
- No attempts yet
Statement
Given an integer , consider the following operation. Let be the largest number that can be obtained by rearranging the digits of , and let be the smallest such number (this number may have leading zeros). Let denote the difference , padded with leading zeros if needed so that it has the same number of digits as .
For example, and .
Kaprekar proved that if you start with a four-digit number whose digits are not all equal and repeatedly apply this operation (), you eventually reach 6174. Since , the process cycles at 6174.
Your task is to write a program that computes for a given number .
Input
The input file contains one integer without leading zeros ().
Output
Print to the output file.