TopCoder problem "IntegerGenerator" used in SRM 281 (Division I Level One , Division II Level Two)



Problem Statement

    

As part of a larger scale project, you need to write a component which generates consecutive positive integers. Only certain digits may appear in the input and in the integers generated, and leading zeros aren't allowed.

You are given a int[] allowed containing the list of allowed digits, and a String current representing the current integer. Return a String representing the first integer larger than current composed only of digits in allowed.

If current represents an invalid integer according to the first paragraph, return "INVALID INPUT" (quotes for clarity).

 

Definition

    
Class:IntegerGenerator
Method:nextInteger
Parameters:int[], String
Returns:String
Method signature:String nextInteger(int[] allowed, String current)
(be sure your method is public)
    
 

Constraints

-allowed will contain between 0 and 10 elements, inclusive.
-Each element in allowed will be between 0 and 9, inclusive.
-allowed will contain no duplicates.
-current will contain between 1 and 10 digits ('0'-'9'), inclusive.
 

Examples

0)
    
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }
"16"
Returns: "17"
With all digits available, the next number is 17.
1)
    
{ 0, 1, 2, 3, 4, 5, 6, 8, 9 }
"16"
Returns: "18"
The digit 7 is no longer allowed, so the next smallest valid integer is 18.
2)
    
{ 3, 5, 8 }
"548"
Returns: "INVALID INPUT"
The current number may not contain disallowed digits.
3)
    
{ 5, 3, 4 }
"033"
Returns: "INVALID INPUT"
Leading zeros aren't allowed either.
4)
    
{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }
"999"
Returns: "1000"
5)
    
{ 0, 1, 2, 3, 4, 5 }
"0"
Returns: "INVALID INPUT"
The generator only works with positive integers.
6)
    
{ 1 }
"1"
Returns: "11"

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=8078&pm=5984

Writer:

lovro

Testers:

PabloGilberto , brett1479 , Olexiy

Problem categories:

Simple Math, String Manipulation