TopcoderArchive
Archive
Algorithm problem

CarParking

Problem statement, definition, constraints, and public examples.

Problem Statement

The town John lives in consists of a large number of streets numbered 1 to streets, inclusive. He insists on parking his car only on street numbers that are palindromic (i.e., they read the same forward and backward). He is currently located at street number now. Return the closest street where he can park his car. If there are multiple solutions, return the smallest among them.

Definition

Class:
CarParking
Method:
closestShed
Parameters:
int, int
Returns:
int
Method signature:
int closestShed(int now, int streets)
(be sure your method is public)

Notes

  • Leading zeroes shouldn't be considered. So you can't interpret "10" as "010" and consider it palindromic.

Constraints

  • streets will be between 1 and 2147483647, inclusive.
  • now will be between 1 and streets, inclusive.

Examples

  1. 19
    22
    Returns: 22
    22 is the closest palindromic street.
  2. 19
    21
    Returns: 11
    Now there is one less street. So the next nearby palindrome is 11.
  3. 1234567
    12345678
    Returns: 1234321
  4. 9999
    99999999
    Returns: 9999
Back to all problems