#C8121. Find the First Repeated Number
Find the First Repeated Number
Find the First Repeated Number
You are given an integer n and n pairs of integers. Your task is to flatten these pairs into a single list and determine the first number (based on its first occurrence) that appears more than once in that list. If no such number exists, output -1
.
Formally, suppose you are given a sequence of pairs:
\[
(a_1, b_1),\ (a_2, b_2),\ \dots,\ (a_n, b_n),
\]
then the flattened list is
\[
a_1,\ b_1,\ a_2,\ b_2,\ \dots,\ a_n,\ b_n.
\]
You need to find the first number in this list that occurs more than once. If every number appears only once, output -1
.
inputFormat
The input is read from standard input and has the following format:
- The first line contains an integer n, the number of pairs.
- Each of the next n lines contains two space-separated integers representing a pair.
outputFormat
Output a single integer (to standard output) which is the first repeated number in the flattened list. If no number repeats, output -1
.
7
1 2
3 4
5 6
7 8
9 10
11 12
13 14
-1