Two wildlife sanctuaries each maintain a log of tag numbers for the migratory birds they have recorded, sorted in non-decreasing order (a tag can appear more than once in a log if the same bird was scanned on separate visits). Conservationists studying cross-site movement want to know the smallest tag number that both sanctuaries have ever recorded, since it identifies the earliest-numbered bird confirmed to have visited both sites.
Given the two logs, find the smallest tag number that appears in both of them, or report that no tag number is shared.
Print a single integer: the smallest tag number that appears in both logs, or -1 if no tag number is shared.
Example 1
Input
3 1 2 3 2 2 4
Expected
2
Explanation
The first sanctuary recorded tags 1, 2, 3 and the second recorded 2, 4. The only tag appearing in both logs is 2, so it is the smallest shared tag.
Example 2
Input
3 1 2 3 3 4 5 6
Expected
-1
Explanation
The logs (1,2,3) and (4,5,6) share no tag number at all, so the answer is -1.
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 →