TopCoder problem "BooksNumbering" used in TCHS SRM 37 (Division I Level Two)



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

0)
    
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.
1)
    
10
Returns: -1
There is no possible solution. It takes 9 digits to label 9 books, and 11 digits to label 10 books.
2)
    
189
Returns: 99
3)
    
192
Returns: 100
4)
    
7
Returns: 7
5)
    
1863927342
Returns: 219448716
6)
    
1863927343
Returns: -1

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=7602

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10776&pm=7602

Writer:

Janq

Testers:

PabloGilberto , brett1479 , Olexiy , ivan_metelsky

Problem categories:

Simple Math, Simple Search, Iteration