A vending machine dispenses one of several ice-cream flavor codes (integers, which may repeat and may be negative, since they are just IDs) every minute, for n minutes total. For a fixed window length k, consider every contiguous window of k consecutive minutes.
For each window, count the number of DISTINCT flavor codes dispensed within it.
Print all counts, in order, one per window, space separated on a single line (there are n - k + 1 counts).
Line 1: two integers n and k.
Line 2: n space-separated integers, the flavor codes.
n - k + 1 space-separated integers: the distinct-flavor count of each window, in order.
Example 1
Input
6 3 1 2 1 3 3 3
Expected
2 3 2 1
Explanation
The windows are [1,2,1] (2 distinct), [2,1,3] (3 distinct), [1,3,3] (2 distinct) and [3,3,3] (1 distinct), giving "2 3 2 1".
Example 2
Input
4 4 5 5 5 5
Expected
1
Explanation
There is only one window (the whole array), and it contains just the single distinct value 5.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →