Connection Status:
Competition Arena > IntegerGenerator
SRM 281 · 2006-01-05 · by lovro · Simple Math, String Manipulation
Class Name: IntegerGenerator
Return Type: String
Method Name: nextInteger
Arg Types: (vector<int>, string)
Problem Statement

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).

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.

Submissions are judged against all 150 archived test cases, of which 6 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class IntegerGenerator with a public method string nextInteger(vector<int> allowed, string current) · 150 test cases · 2 s / 256 MB per case

Submitting as anonymous