Difference from Reversal
Time limit2sMemory limit128 MB
Find the smallest nonnegative integer x whose value minus its decimal reversal equals a given natural number D, or report that none exists.
- Level
Medium7 of 10
- Topics
- Math, Brute force, Number theory, Implementation
- Solved
- No attempts yet
Problem
For a nonnegative integer, define its reversal as the integer obtained by reading its decimal digits from right to left. Any zeros that become leading zeros after the reversal disappear when the value is interpreted as an integer.
Given a natural number D, find the smallest nonnegative integer x such that x - reverse(x) = D. Here, reverse(x) denotes the reversal of x.
Input
D is given. D is a natural number not greater than 1,000,000.
Output
Print the smallest x satisfying the condition. If no such x exists, print -1.