#K15091. Max Length Contiguous Subarray with At Most K Distinct Integers

    ID: 24279 Type: Default 1000ms 256MiB

Max Length Contiguous Subarray with At Most K Distinct Integers

Max Length Contiguous Subarray with At Most K Distinct Integers

You are given an integer (K) and an array (A) of integers. Your task is to find the maximum length (L) of a contiguous subarray of (A) that contains at most (K) distinct integers. In other words, for any contiguous segment (A[i \ldots j]) where the number of distinct integers is ( \le K ), determine the maximum possible value of (j - i + 1).

This problem can be formally stated as: Given an array (A) of length (n) and an integer (K), find [ L = \max_{0 \le i \le j < n} { j - i + 1 \ | \ |{ A[i], A[i+1], \dots, A[j] }| \le K }. ] If (K = 0), then no elements are allowed and the answer is 0.

inputFormat

The input is read from standard input (stdin) and has the following format:

T (number of test cases)
For each test case:
    K N     (an integer K and an integer N denoting the length of the array)
    a[1] a[2] ... a[N]     (N space-separated integers representing the array)

Each test case is processed independently.

outputFormat

For each test case, output one line containing a single integer which is the maximum length of a contiguous subarray that contains at most K distinct integers. The output is written to standard output (stdout).## sample

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

5 4

</p>