#K74492. Smallest Subarray with K Unique Elements
Smallest Subarray with K Unique Elements
Smallest Subarray with K Unique Elements
Given an array of n integers, the task is to find the length of the smallest contiguous subarray that contains at least k unique elements. If no such subarray exists, output -1.
A subarray is defined as a contiguous segment of the array. Formally, if S is a subarray of A, then the uniqueness of S is given by $$\text{Unique}(S)=\left|\{x\, :\, x\in S\}\right|.$$ Your goal is to determine the minimum length L such that there exists a subarray S with length L and $$\text{Unique}(S)\ge k.$$
If no subarray satisfies the condition, output -1.
inputFormat
The first line contains two integers n
and k
separated by a space.
The second line contains n
integers representing the array elements, separated by spaces.
Input is given via standard input (stdin).
outputFormat
Output a single integer which is the length of the smallest subarray that contains at least k
unique elements, or -1 if no such subarray exists.
Output should be written to standard output (stdout).
## sample7 3
1 2 1 2 3 2 3
3