#K39657. Longest Subarray with At Most K Distinct Integers
Longest Subarray with At Most K Distinct Integers
Longest Subarray with At Most K Distinct Integers
You are given an array of n integers and an integer k. Your task is to determine the length of the longest continuous subarray that contains at most k distinct integers.
In other words, given an array ( a_1, a_2, \ldots, a_n ) and an integer ( k ), find the maximum length ( L ) such that there exists some contiguous segment ( a_i, a_{i+1}, \ldots, a_{i+L-1} ) which contains no more than ( k ) distinct numbers.
Input is read from standard input (stdin) and the result should be printed to standard output (stdout).
inputFormat
The first line contains two integers ( n ) and ( k ) (1 ≤ n ≤ 10^5, 1 ≤ k ≤ n), where ( n ) is the number of elements in the array and ( k ) is the maximum number of distinct integers allowed. The second line contains ( n ) space-separated integers representing the elements of the array.
outputFormat
Output a single integer representing the length of the longest subarray that contains at most ( k ) distinct integers.## sample
7 2
1 2 1 2 3 3 4
4