#K73762. Minimum Length Contiguous Segment Covering All Genres

    ID: 34048 Type: Default 1000ms 256MiB

Minimum Length Contiguous Segment Covering All Genres

Minimum Length Contiguous Segment Covering All Genres

You are given T test cases. In each test case, you are provided with a sequence of performances at different stages. Each performance is associated with a specific genre.

The first line of each test case contains two integers: N (the number of stages) and M (the number of distinct genres). The second line contains N integers, where the i-th integer represents the genre of the performance at stage i.

Your task is to determine the minimum length of a contiguous segment (i.e. a subarray) such that the segment contains at least one performance from every genre (i.e. all M genres are present). If no such segment exists, output -1.

The problem can be mathematically formulated as follows: Given an array A of length \( N \) and an integer \( M \), find the minimum \( L \) such that there exists indices \( i \) and \( j \) with \( j - i + 1 = L \) and \( \{A[i], A[i+1], \ldots, A[j]\} \) contains all genres \( \{1,2,\ldots, M\} \). If no such segment exists, return \(-1\).

inputFormat

The first line of input contains an integer T denoting the number of test cases.

Each test case consists of the following:

  • A line with two integers N and M, where N is the number of stages and M is the number of genres.
  • A line with N space-separated integers representing the genre of each performance.

Note: Input is provided via standard input (stdin).

outputFormat

For each test case, output a single integer on a new line representing the minimum length of a contiguous segment that covers all M genres. If no such segment exists, output -1.

Note: Output should be written to standard output (stdout).

## sample
2
8 3
1 2 2 3 1 1 3 2
5 4
1 2 1 2 1
3

-1

</p>