Two adjoining vault doors must be opened by entering a single numeric access code. Door A's keypad only recognizes digits from a set A, and Door B's keypad only recognizes digits from a set B (each set lists distinct digits from 1 to 9). A code satisfies both doors if it contains at least one digit that belongs to set A and at least one digit that belongs to set B — a single digit that happens to belong to both sets can satisfy both requirements by itself. Find the smallest positive integer access code that satisfies both doors: fewer digits always wins (a one-digit code beats any two-digit code), and among codes of the same digit count, the numerically smallest one is required.
nA followed by nA space-separated distinct integers — the digits in set A.nB followed by nB space-separated distinct integers — the digits in set B.Print the smallest access code (as a plain integer, no leading zeros) that contains at least one digit from set A and at least one digit from set B.
Example 1
Input
3 4 3 2 3 5 4 6
Expected
4
Explanation
Set A = {4,3,2} and set B = {5,4,6} share the digit 4 (and only 4). A single-digit code "4" is recognized by both keypads at once, and since no smaller shared digit exists, "4" is the smallest possible satisfying code.
Example 2
Input
3 5 3 8 3 7 1 6
Expected
13
Explanation
Set A = {5,3,8} and set B = {7,1,6} have no digit in common, so no one-digit code can satisfy both doors. The smallest usable digit from A is 3 and from B is 1; placing the smaller of the two (1) first and the larger (3) second gives the code "13", which is smaller than any other pairing such as "31" or using non-minimal digits from either set.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →