Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

The Smallest Window II

For a given array $a_1, a_2, a_3, ... , a_N$ of $N$ elements and an integer $K$, find the smallest sub-array size (smallest window length) where the elements in the sub-array contains all integers in range [$1, 2, ..., K$]. If there is no such sub-array, report 0.

Constraints

$1 \leq N \leq 10^5$

$1 \leq K \leq 10^5$

$1 \leq a_i \leq 10^5$

Input

The input is given in the following format.

$N$ $K$

$a_1$ $a_2$ ... $a_N$

Output

Print the smallest sub-array size in a line.

Sample Input 1

6 2
4 1 2 1 3 5

Sample Output 1

2

Sample Input 2

6 3
4 1 2 1 3 5

Sample Output 2

3

Sample Input 3

3 4
1 2 3

Sample Output 3

0

Solution in Python3 (judge verdict: Accepted)

import sys

n,k = map(int, input().split( ))
a = list(map(int, input().split( )))
tmp = max(a)
tmp2 = max(tmp,n,k)+1
chk = [0]*(tmp2)

for i in range(n):
    chk[a[i]] += 1

if 0 in chk[1:k+1]:
    print(0)
    sys.exit()

right = n-1
left = 0
L = []

while right < n:
    while chk[a[right]] > 1 or a[right] > k:
        chk[a[right]] -= 1
        right -= 1
        
    ln = right - left + 1
    L.append(ln)
    
    while chk[a[left]] > 1 or a[left] > k:
        chk[a[left]] -= 1
        left += 1
    ln = right - left + 1
    L.append(ln)
    chk[a[left]] -= 1
    left += 1
    while chk[a[left-1]] == 0:
        right += 1
        if right >= n:
            break
        chk[a[right]] += 1
    ln = right -left +1
    L.append(ln)
print(min(L))