#C3528. Smallest Subarray with K Distinct Integers
Smallest Subarray with K Distinct Integers
Smallest Subarray with K Distinct Integers
Given an array of (n) integers and an integer (k), find the length of the smallest contiguous subarray that contains at least (k) distinct integers. If such a subarray does not exist, output (-1).
The goal is to identify the minimal length (L) such that there exists an index (i) where the subarray (a_i, a_{i+1}, \ldots, a_{i+L-1}) has at least (k) unique integers. This problem can be efficiently tackled using a sliding window technique.
Example:
For the input array [1, 2, 4, 2, 2, 3, 1] with (k = 3), the smallest subarray with 3 distinct integers is of length 3 (e.g., [4, 2, 2] or [2, 2, 3]).
inputFormat
The input is read from standard input (stdin).
The first line contains two integers (n) and (k), where (n) is the number of elements in the array and (k) is the target number of distinct integers.
The second line contains (n) space-separated integers representing the array.
outputFormat
Output a single integer on standard output (stdout): the length of the smallest subarray that contains at least (k) distinct integers. If no such subarray exists, output (-1).## sample
7 3
1 2 4 2 2 3 1
3