TopcoderArchive
Archive
Algorithm problem

BooksNumbering

Problem statement, definition, constraints, and public examples.

Problem Statement

A new library was founded in our city. The books were labeled 1, 2, 3, 4, etc., with no leading zeroes. A total of usedDigits digits were used to create the labels. Return the total number of books in the library, or -1 if usedDigits does not correspond to a valid solution.

Definition

Class:
BooksNumbering
Method:
numberOfBooks
Parameters:
int
Returns:
int
Method signature:
int numberOfBooks(int usedDigits)
(be sure your method is public)

Constraints

  • usedDigits will be between 1 and 2 * 109, inclusive.

Examples

  1. 11
    Returns: 10
    Each of the first 9 numbers (1-9) contain one digit. Number 10 contains 2 digits. Therefore, there's a total of 11 digits.
  2. 10
    Returns: -1
    There is no possible solution. It takes 9 digits to label 9 books, and 11 digits to label 10 books.
  3. 189
    Returns: 99
  4. 192
    Returns: 100
  5. 7
    Returns: 7
  6. 1863927342
    Returns: 219448716
  7. 1863927343
    Returns: -1
Back to all problems