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: Wrong Answer)

using System;
using System.Linq;//リストの使用
using System.Collections.Generic;
class Program
{
static string[] input = Console.ReadLine().Split(' ');//Splitで区切り文字を指定して複数個受け取る。
static int n = int.Parse(input[0]);
static int k = int.Parse(input[1]);
static int[] nums = Array.ConvertAll(Console.ReadLine().Split(' '),int.Parse);
static int[] bitsOne = new int[100000+1];
static int[] bits = new int[100000+1];

	static void Main()
	{
    int answer = int.MaxValue/2;
    int leftCount = 0;
  
    for(int i = 1; i <= n; i++)
    {
      BitAdd(nums[i-1],1);
      if(BitSumOne(nums[i-1]) - BitSumOne(nums[i-1]-1) == 0) BitAddOne(nums[i-1],1);

      while(BitSumOne(nums[i-1]) == k)
      {
        answer = Math.Min(answer, i-leftCount);
        BitAdd(nums[leftCount], -1);
          if(BitSum(nums[leftCount]) - BitSum(nums[leftCount]-1) == 0)
          {
            BitAddOne(nums[leftCount], -1);
          }
        //Console.WriteLine(answer);
        leftCount++;
      }
    }

    if(answer == int.MaxValue/2) answer = 0;
		Console.WriteLine(answer);
	}

  static void BitAdd(int i, int x)//BITでiだけにx加算
  {
    while(i <= 100000)
    {
      bits[i] += x;
      i += i&-i;
    }
  }

  static int BitSum(int i)//BITでi以下の総和を計算
  {
    int sum = 0;
    while(i > 0)
    {
      sum += bits[i];
      i -= i&-i;
    }
    return sum;
  }

  static void BitAddOne(int i, int x)//BITでiだけにx加算
  {
    while(i <= 100000)
    {
      bitsOne[i] += x;
      i += i&-i;
    }
  }

  static int BitSumOne(int i)//BITでi以下の総和を計算
  {
    int sum = 0;
    while(i > 0)
    {
      sum += bitsOne[i];
      i -= i&-i;
    }
    return sum;
  }
  
}