TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

NiceNumbers

Problem statement, definition, constraints, and public examples.

Problem Statement

When you register on many Internet services, you receive a user number which becomes your ID. The numbers are assigned to the registrants sequentially, so the next registrant receives the next number, numerically. For instance, if the last given number was 12, then the next will be 13, then 14, and so forth.

Since this number is your ID, it's of course nice to have a number that's easy to remember. For instance, the number 500000 is much easier to remember than 829531. Some people, who really want an easy to remember number (we call such numbers nice numbers), register several times just to get a nice number as an ID.

A nice number is a number that meets at least one of the following criteria:

  • A number that contains exactly one digit that is not a 0 (e.g. 500, 300000, 8)
  • A number that contains at most two distinct digits (e.g. 117177, 666, 9888898)
  • A number that is a palindrome; that is, the number is the same if the digits are reversed (e.g. 86368, 99, 17333371)
  • A number where all pairs of adjacent digits differ by exactly 1 (e.g. 12343, 7678, 987654321)

Create a class NiceNumber containing the method howMany which takes as input an int n, the ID number you received when registering the first time, and returns an int representing how many more (if any) times you must register until you reach a nice number. If n is already a nice number, return 0. You may assume that no one else will attempt to register while you're pulling this scam.

Definition

Class:
NiceNumbers
Method:
howMany
Parameters:
int
Returns:
int
Method signature:
int howMany(int n)
(be sure your method is public)

Notes

  • Do not consider any extra leading zeros when determining if a number is a nice number.
  • The solution can never be greater than 110000.

Constraints

  • n is between 1 and 2,000,000,000, inclusive.

Examples

  1. 40000
    Returns: 0
    40000 is already a nice number, so the method returns 0.
  2. 29915
    Returns: 7
    The first nice number that shows up will be 29922 (containing at most two different digits).
  3. 234793273
    Returns: 4159
    After registering 4159 more times, you receive the ID 234797432, which is a palindrome.
  4. 78765123
    Returns: 309
    The nice number will be 78765432.
Back to all problems