#K84422. Longest Valid Segment

    ID: 36416 Type: Default 1000ms 256MiB

Longest Valid Segment

Longest Valid Segment

You are given a forest represented as an array of n integers, and an integer k which represents the number of distinct plant types that are expected to appear in a contiguous segment. Your task is to find the length of the longest contiguous segment in the forest such that each of the k distinct plant types appears exactly two times.

In other words, if you denote the segment by indices i to j (inclusive), then for every plant type in that segment, its frequency must be exactly two. Mathematically, if F(x) is the frequency of plant type x in the segment, then the segment is valid if and only if $$ \forall x \text{ in the segment},\ F(x)=2. $$

If no valid segment exists, output -1.

Note: Since a valid segment must contain exactly two occurrences of each type, the length of any valid segment is exactly 2*k if such a segment exists.

inputFormat

The first line of input contains two integers n and k, where n is the number of elements in the forest, and k is the number of distinct plant types that must appear exactly twice in a valid segment.

The second line contains n space-separated integers representing the forest.

outputFormat

Output a single integer representing the length of the longest contiguous segment where every one of the k types appears exactly twice. If no such segment exists, output -1.

## sample
10 3
3 2 3 1 1 2 1 2 3 1
6