Distinct Elements in a Sliding WindowMedium

Distinct Elements in a Sliding Window

MediumPractice library · +20 XParrayshash mapsliding window

Given an array of N integers and an integer K, find the number of distinct elements in each contiguous subarray of size K. Print these counts in the order the subarrays appear.

Input

The first line contains two integers N and K, separated by a space. The second line contains N integers, the elements of the array.

Output

Print N-K+1 integers, the number of distinct elements in each subarray of size K.

Constraints

1 <= N <= 200000, 1 <= K <= N, The elements of the array are integers between -10^9 and 10^9.

Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests

Example 1

Input
8 3
1 2 1 3 4 2 3 1
Output
2 3 3 3 3 3

The subarrays of size 3 are [1,2,1] (2 distinct), [2,1,3] (3 distinct), [1,3,4] (3 distinct), [3,4,2] (3 distinct), [4,2,3] (3 distinct), [2,3,1] (3 distinct).

Example 2

Input
5 5
10 20 20 10 30
Output
3

There is only one subarray of size 5: [10, 20, 20, 10, 30], which has 3 distinct elements.

Hints