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 C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Globalization;
using System.Diagnostics;
using Pair = System.Collections.Generic.KeyValuePair<int, int>;

class Program
{
    static void Main()
    {
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        new Program().Solve();
        Console.Out.Flush();
    }
    Scanner cin = new Scanner();


    void Solve()
    {
        int N = cin.Nextint;
        long K = cin.Nextint;
        var A = cin.Scanint;
        var ans = int.MaxValue;
        int l = 0;
        int r = 0;

        var B = new int[K + 1];
        var remain = (1 + K) * K / 2;
        while (l < N)
        {
            while (r < N)
            {
                if (A[r] <= K)
                {
                    if (B[A[r]] == 0)
                    {
                        remain -= A[r];
                    }
                    B[A[r]]++;
                }
                if (remain == 0)
                {
                    break;
                }
                r++;
            }
            //WriteLine($"l={l},r={r}");
            if (r == N)
            {
                break;
            }

            while (l <= r)
            {
                if (A[l] <= K)
                {
                    B[A[l]]--;
                    if (B[A[l]] == 0)
                    {
                        ans = Math.Min(ans, r - l + 1);
                        remain += A[l];
                        break;
                    }
                }
                l++;
            }
            l++;
            r++;
        }
        Console.WriteLine(ans == int.MaxValue ? 0 : ans);
    }

}

class Scanner
{
    string[] s; int i;
    char[] cs = new char[] { ' ' };
    public Scanner() { s = new string[0]; i = 0; }
    public string[] Scan { get { return Console.ReadLine().Split(); } }
    public int[] Scanint { get { return Array.ConvertAll(Scan, int.Parse); } }
    public long[] Scanlong { get { return Array.ConvertAll(Scan, long.Parse); } }
    public double[] Scandouble { get { return Array.ConvertAll(Scan, double.Parse); } }
    public string Next
    {
        get
        {
            if (i < s.Length) return s[i++];
            string st = Console.ReadLine();
            while (st == "") st = Console.ReadLine();
            s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
            i = 0;
            return Next;
        }
    }
    public int Nextint { get { return int.Parse(Next); } }
    public long Nextlong { get { return long.Parse(Next); } }
    public double Nextdouble { get { return double.Parse(Next); } }
}