Connection Status:
Competition Arena > PalindromizationDiv2
SRM 509 · 2010-11-01 · by nV · Brute Force, Simple Search, Iteration
Class Name: PalindromizationDiv2
Return Type: int
Method Name: getMinimumCost
Arg Types: (int)
Problem Statement

Problem Statement

Little Arthur loves numbers, especially palindromic ones. A palindromic string is a string that reads the same both forwards and backwards. A palindromic number is a non-negative integer such that its decimal representation (without insignificant leading zeros) is a palindromic string. For example, 12321, 101, 9, and 0 are palindromic numbers but 2011, 509, and 40 are not.

Arthur has a number X and he would like to palindromize it. Palindromization of a number means adding or subtracting some value to obtain a palindromic number. For example, one possible way to palindromize number 25 is adding 8 resulting in number 33, which is palindromic.

Unfortunately Arthur cannot palindromize numbers for free. The cost of palindromization in dollars is equal to the value added or subtracted. In the previous example Arthur would have to pay 8 dollars.

Of course Arthur would like to palindromize X spending the least amount of money. Given X return the minimum amount of money Arthur needs.

Constraints

  • X will be between 0 and 100000 (10^5), inclusive.
Examples
0)
25
Returns: 3

In the problem statement it is shown that adding 8 to 25 results in a palindromic number 33. However, this is not the cheapest way to palindromize 25. Arthur can subtract 3 to obtain a number 22 which is also palindromic.

1)
12321
Returns: 0

Already a palindromic number.

2)
40
Returns: 4
3)
2011
Returns: 9
4)
0
Returns: 0

Submissions are judged against all 143 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class PalindromizationDiv2 with a public method int getMinimumCost(int X) · 143 test cases · 2 s / 256 MB per case

Submitting as anonymous