#K4341. Find the Rare Manuscript Identifier
Find the Rare Manuscript Identifier
Find the Rare Manuscript Identifier
You are given M collections of positive integers. Your task is to find the Rare Manuscript Identifier (RMI), which is defined as an integer that appears exactly once in each collection.
More formally, let \(C_i\) be the \(i\)-th collection, and let \(\text{count}(x, C_i)\) denote the number of occurrences of integer \(x\) in collection \(C_i\). An integer \(x\) is considered as a candidate for the RMI if and only if:
\(\forall i \in \{1,2,\ldots,M\},\; \text{count}(x, C_i) = 1\)
If there is exactly one such candidate, print its value. Otherwise, print -1
.
Note: There might be multiple numbers that satisfy the condition, in which case the answer is -1
. Similarly, if no number satisfies this criterion, print -1
.
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains an integer M, the number of collections.
- Each of the next M lines contains a collection of integers separated by spaces.
It is guaranteed that each collection contains at least one integer.
outputFormat
Output a single integer to standard output (stdout) representing the Rare Manuscript Identifier if it exists uniquely according to the conditions. Otherwise, output -1
.
3
11 23 11 45
23 45 11 11
11 23 33 23
-1
</p>