Distinct Elements in Sliding WindowMedium

Distinct Elements in Sliding Window

MediumPractice library · +20 XPstackhash mapsliding window

Given an array of N integers and an integer K, find the number of distinct elements in each contiguous subarray (window) of size K. The windows are formed by sliding a window of size K from the beginning to the end of the array. The first window is arr[0..K-1], the second is arr[1..K], and so on, until the last window arr[N-K..N-1].

Input

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

Output

Print N-K+1 integers, separated by spaces, where each integer is the count of distinct elements in the corresponding window.

Constraints

1 <= K <= N <= 200000 -10^9 <= arr[i] <= 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

Windows: [1,2,1]->2 distinct, [2,1,3]->3, [1,3,4]->3, [3,4,2]->3, [4,2,3]->3, [2,3,1]->3

Example 2

Input
5 2
10 10 10 10 10
Output
1 1 1 1

Windows: [10,10]->1 distinct, [10,10]->1, [10,10]->1, [10,10]->1

Hints