#K71797. Largest Subarray with At Most K Distinct Integers

    ID: 33611 Type: Default 1000ms 256MiB

Largest Subarray with At Most K Distinct Integers

Largest Subarray with At Most K Distinct Integers

Given an array of non-negative integers and an integer \(k\), your task is to find the largest possible subarray (i.e. a contiguous segment) that contains at most \(k\) distinct integers. In other words, for a given array \(A\) of length \(n\), find the subarray \(A[l..r]\) with maximum length such that \[ |\{A_i : l \le i \le r\}| \le k, \] where \(|\cdot|\) denotes the cardinality of a set. If there are multiple subarrays with the same maximum length, choose the one starting at the smallest index. If the array is empty or \(k = 0\), output an empty line.

Note: The input consists of multiple test cases. Your solution should read from standard input and output the result for each test case on a new line.

inputFormat

The first line contains an integer (T) denoting the number of test cases. For each test case, the first line contains two integers (n) and (k), where (n) is the length of the array and (k) is the maximum number of distinct integers allowed. The second line contains (n) space-separated non-negative integers representing the array.

outputFormat

For each test case, output a single line containing the elements of the largest subarray (space-separated) that contains at most (k) distinct integers. If the subarray is empty, print an empty line.## sample

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

1 2 1 3

</p>