#K42457. Taco - The Smallest Window Problem

    ID: 27091 Type: Default 1000ms 256MiB

Taco - The Smallest Window Problem

Taco - The Smallest Window Problem

Given an array of integers, find the length of the smallest contiguous subarray (window) which contains all the distinct elements present in the array. In other words, if the set of distinct elements of the array is \(D\) with \(|D| = k\), you need to determine the minimum length \(L\) such that there exists a subarray of length \(L\) that includes every element from \(D\).

Example: For the array [1, 2, 2, 3, 2, 1, 4], the set of distinct elements is {1, 2, 3, 4} (i.e. \(k=4\)). The smallest window containing all these elements is [2, 3, 2, 1] or [3, 2, 1, 4] with length 4.

The problem requires you to process multiple test cases. For each test case, you are given the number of elements followed by the list of integers.

inputFormat

The input begins with an integer (T) denoting the number of test cases. Each test case consists of two lines. The first line contains a single integer (n) — the number of elements in the array. The second line contains (n) space-separated integers which represent the elements of the array.

outputFormat

For each test case, output a single integer — the length of the smallest contiguous subarray that contains all distinct elements of the given array. Each output should be on its own line.## sample

3
7
1 2 2 3 2 1 4
4
1 2 2 2
5
1 2 3 4 5
4

2 5

</p>