#P8986. Alien Gene Editing Minimization
Alien Gene Editing Minimization
Alien Gene Editing Minimization
An alien civilization, HD1048576d, uses a gene editing technique on their life forms. Their genetic material is represented as a sequence of positive integers (each integer represents a noicleobase). Their editing process is as follows:
- Select an editing region [l, r] in the sequence, i.e. the segment sl, sl+1, ... , sr will be replaced.
- Choose a pair of indices (i, j) with 1 ≤ i < l and r < j ≤ n so that the subsequence si, si+1, ... , sj is processed. In the new sequence this segment becomes si, si+1, ... , sl-1, t1, t2, ... , tk, sr+1, ... , sj, where t1, ... , tk is newly produced.
- After cutting out si ... sj, the processed segment is reattached to form the final target gene sequence.
For the operation to be specific, the chosen pair of indices (i, j) must satisfy two key conditions:
- Editing gap: There must exist positions l and r with i < l and r < j. This implies that j - i ≥ 2.
- Uniqueness of recognition: The pair of noicleobases at the ends, i.e. (si, sj), must be unique in the whole sequence. More formally, if there exists any other ordered pair of indices (i', j') (with i' < j') such that si' = si and sj' = sj, then the pair (si, sj) is not suitable. Also, note that si ≠ sj must hold.
The cost of editing is proportional to the length of the processed segment si ... sj, that is, its length j - i + 1. The goal is to minimize this length among all valid pairs (i, j) satisfying the conditions above. If no such pair exists, output -1.
Input and Output in summary:
Given a gene sequence of length n represented by s1, s2, ... , sn, find the minimal length j - i + 1 (with j - i ≥ 2) such that:
- si ≠ sj, and
- the ordered pair (si, sj) appears exactly once among all pairs of indices (i, j) with 1 ≤ i < j ≤ n.
If no valid pair exists, output -1.
Note: All formulas are given in LaTeX format. For example, the condition j - i \ge 2
is written in LaTeX as shown.
inputFormat
The first line contains an integer n (n ≥ 1), the length of the gene sequence.
The second line contains n positive integers separated by spaces, representing the gene sequence s1, s2, ..., sn.
outputFormat
Output a single integer, the minimum possible length of the subsequence si ... sj (i.e. j - i + 1) that can be processed under the constraints. If no valid pair exists, output -1.
sample
5
1 2 3 4 5
3