#K80182. Count Distinct Elements in Subarrays

    ID: 35474 Type: Default 1000ms 256MiB

Count Distinct Elements in Subarrays

Count Distinct Elements in Subarrays

Your task is to compute the number of unique elements in every contiguous subarray (or window) of size \( k \) for a given array \( arr \). An efficient solution is expected, ideally using a sliding window technique.

For example, consider the array [4, 1, 1, 2, 3, 4, 5] with \( k = 3 \). The first window [4, 1, 1] contains 2 distinct elements, the second window [1, 1, 2] has 2 distinct elements, and so on, resulting in the output [2, 2, 3, 3, 3].

If \( k \) is greater than the length of the array, there are no subarrays of size \( k \), and the output should be empty.

inputFormat

The input is given via standard input (stdin) as follows:

  • The first line contains two integers \( n \) and \( k \) separated by a space, representing the size of the array and the subarray size respectively.
  • The second line contains \( n \) space-separated integers, representing the elements of the array \( arr \).

outputFormat

The output should be printed to standard output (stdout) as a single line containing the counts of unique elements in each contiguous subarray of size ( k ), separated by a space. If there are no subarrays of size ( k ), simply output an empty line.## sample

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