#K71457. Distinct Elements in Subarrays
Distinct Elements in Subarrays
Distinct Elements in Subarrays
You are given an integer array arr
of size N and an integer K. Your task is to count the number of distinct elements in every contiguous subarray (or window) of size K.
If K > N
or the array is empty, then the output should be empty. Otherwise, for each contiguous subarray of size K, compute the number of unique elements and print them in order, separated by spaces.
Note: Use efficient data structures to ensure that the solution works well for large inputs.
Example:
Input: 7 1 2 1 3 4 2 3 4</p>Output: 3 4 4 3
inputFormat
The input is read from standard input and consists of three parts:
- An integer N representing the size of the array.
- A line of N space-separated integers, representing the elements of the array.
- An integer K denoting the size of the subarray.
If N = 0
or K > N
, print an empty output.
outputFormat
Print to standard output the count of distinct elements in every contiguous subarray of size K. The counts should be printed as space-separated integers in one line. If there is no valid subarray, print nothing.
## sample7
1 2 1 3 4 2 3
4
3 4 4 3
</p>