#K82352. Longest Subarray with Exactly K Distinct Elements
Longest Subarray with Exactly K Distinct Elements
Longest Subarray with Exactly K Distinct Elements
You are given an array of N integers and an integer K. Your task is to find the length of the longest contiguous subarray that contains exactly K distinct integers.
If there is no such subarray, output 0
.
Note: The answer must be computed by reading input from standard input (stdin
) and writing the result to standard output (stdout
).
Input Format: The first line contains two integers N and K. The second line contains N space-separated integers representing the array.
Output Format: Output a single integer — the length of the longest contiguous subarray with exactly K distinct integers.
inputFormat
The input consists of two lines:
- The first line contains two integers N (the size of the array) and K (the required number of distinct integers).
- The second line contains N space-separated integers, representing the elements of the array.
outputFormat
Output a single integer which is the length of the longest contiguous subarray that contains exactly K distinct integers. Output 0
if no such subarray exists.
10 2
1 2 1 2 3 4 3 5 6 1
4
</p>