TopcoderARCHIVE
Archive/Problems/AddReverse
TCO · Problem 17914

AddReverse

Problem statement, definition, constraints, and public examples.

Problem Statement

The reverse of a positive integer X, denoted rev(X), is the number formed by the same digits but in the opposite order.

If a number with trailing zeros is reversed, the trailing zeros become leading zeros and thus they are discarded. For example, rev(4700) = 0074 = 74.

Adding leading zeros before making the reversal is not allowed, so the value rev(X) is always unique.


The String N contains the canonical base-10 representation of a positive integer. Let int(N) denote that integer.

Determine whether there is a positive integer X such that int(N) = X + rev(X). If yes, return a String containing the canonical representation of any one such X. If no, return an empty String instead.

Definition

Class:
AddReverse
Method:
solve
Parameters:
String
Returns:
String
Method signature:
String solve(String N)
(be sure your method is public)

Notes

  • "Canonical representation" means that there are no leading zeros.

Constraints

  • N will contain between 1 and 5,000 characters, inclusive.
  • Each character of N will be a digit ('0'-'9').
  • The first character of N will not be '0'.

Examples

  1. "88"
    Returns: "44"
    If we take X = 44, we have rev(X) = 44, and 44 + 44 = 88. Several other correct X exist.
  2. "11"
    Returns: "10"
    There is only one way to get the sum 11: we need to choose X = 10 and then we add rev(X) = 01 = 1. Thus, "10" is the only correct return value.
  3. "121"
    Returns: "110"
    Here, X can be any one of the following values: { 29, 38, 47, 56, 65, 74, 83, 92, 110 }. For example, 47 + rev(47) = 47 + 74 = 121, and 110 + rev(110) = 110 + 11 = 121.
  4. "1000"
    Returns: ""
    There is no X such that X + rev(X) = 1000.
  5. "10485827274850"
    Returns: "7382858692013"
← All problems