Smallest number missing from a digit sequence
InterviewTime limit1sMemory limit256 MB
Given up to 1000 digits, find the smallest non-negative integer that never occurs as a consecutive block without a leading zero.
- Level
Medium4 of 10
- Topics
- String matching, Brute force
- Solved
- No attempts yet
Problem
Hanako heard that if you pick any non-negative integer, it always turns up somewhere in the decimal expansion of . Since then, whenever she sees a sequence of digits she checks which integers appear in it.
An integer appears in the sequence if you can read it off consecutive digits, in order, as a decimal number. A value of two or more digits must not start with 0. For example, the sequence 3 0 1 contains the integers 3, 0, 1, 30 and 301, five in total.
Hanako wants the smallest non-negative integer that does not appear in the sequence. The sequence above contains 0 and 1 but not 2, so the answer is 2.
Write a program that answers Hanako's question.
Input
The first line contains the length of the sequence ().
From the second line on, are given in order. Each is an integer between 0 and 9, and neighbouring digits are separated by a space or a line break.
Output
Print the smallest non-negative integer that does not appear in the sequence.