#K43412. Counting Distinct Integers in Subarrays
Counting Distinct Integers in Subarrays
Counting Distinct Integers in Subarrays
Given an array of integers and an integer \( k \), your task is to count the number of distinct integers in every contiguous subarray of length \( k \). Formally, for an array \( A \) of length \( n \), for each subarray \( A[i..i+k-1] \) (with \( 0 \leq i \leq n-k \)), compute:
\[ \text{count} = \left|\{A[i], A[i+1], \dots, A[i+k-1]\}\right| \]
For example, if \( A = [1, 2, 1, 3, 4, 2, 3] \) and \( k = 4 \), then the subarrays are \([1, 2, 1, 3]\), \([2, 1, 3, 4]\), \([1, 3, 4, 2]\), and \([3, 4, 2, 3]\) with distinct counts of 3, 4, 4, 3 respectively.
inputFormat
The input is read from standard input (stdin). The first line contains two integers ( n ) and ( k ), where ( n ) is the number of elements in the array and ( k ) is the size of the subarray. The second line contains ( n ) space-separated integers that represent the array.
outputFormat
Print a single line to standard output (stdout) containing the distinct counts for each contiguous subarray of length ( k ), separated by spaces. If ( k ) is greater than ( n ), print an empty line.## sample
7 4
1 2 1 3 4 2 3
3 4 4 3