TopcoderArchiveVisit Topcoder
SRM1 Nov 2010
SRM 509

PalindromizationDiv2

Problem statement, definition, constraints, and public examples.

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.

Definition

Class:
PalindromizationDiv2
Method:
getMinimumCost
Parameters:
int
Returns:
int
Method signature:
int getMinimumCost(int X)
(be sure your method is public)

Constraints

  • X will be between 0 and 100000 (10^5), inclusive.

Examples

  1. 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.
  2. 12321
    Returns: 0
    Already a palindromic number.
  3. 40
    Returns: 4
  4. 2011
    Returns: 9
  5. 0
    Returns: 0
Back to all problems