Countries own a series of territories on a 1D world. Each country is uniquely identified by a number. Ownership of the territories can be represented by a list as follows:
1 1 2 2 1 3 3 2 4
We define a country's edgemost territories as the two territories closest to either edge. If the above list was zero indexed, country 1
's edgemost territories occur at position 0
and 4
.
A country surrounds another if the sublist between its two edgemost territories contains all the territories of another country. In the above example, the sublist between country 2
's edgemost territories is:
2 2 1 3 3 2
And we see that all the territories of country 3
are between the edgemost territories of country 2
, so country 2
surrounds country 3
.
A country with only one element will never surround another.
Challenge
Take a list of integers as input (in any format) and output a truthy value if any country is surrounded by another, and a falsy value otherwise.
You can assume that the input list is nonempty, only contains positive integers, and does not 'skip' any numbers: for example, 1 2 1 5
would be invalid input.
Test Cases
+----------------------+--------+
| Input | Output |
+----------------------+--------+
| 1 | False |
| 2 1 3 2 | True |
| 2 1 2 1 2 | True |
| 1 2 3 1 2 3 | False |
| 1 3 1 2 2 3 2 3 | True |
| 1 2 2 1 3 2 3 3 4 | False |
| 1 2 3 4 5 6 7 8 9 10 | False |
+----------------------+--------+