Problem:
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

Submitted solutions:

code_snippet: ///***.......      In the name of Allah     ......***////
/*...Free Palestine..........Free Al_Aqsa.....*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define yes cout<<"YES"<<endl
#define no  cout<<"NO"<<endl
#define pb push_back
#define tt int t;cin>>t;while(t--)
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define loop(i,n) for(int i=0;i<n;i++)
#define input(x) for(int i=0;i<n;i++)cin>>x[i]
#define srt(x) sort(x.begin(),x.end())
#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n' 
void solve()
{
 int n,k;
 cin>>n>>k;
 vector<int>v(n);loop(i,n)cin>>v[i];
 map<int,int>ce;
 for(int i=1;i<=k;i++)
 {
    ce[i]++;
 }
 int cnt=0;
 int l=0;
 int r=0;
 int ans=n+1;
 map<int ,int>m;
 while(r<n)
 {
     m[v[r]]++;
    if(m[v[r]]==1&&ce[v[r]]==1)
    {
        cnt++;
    }
    if(cnt==k)
    {
        while(m[v[l]]>ce[v[l]])
        {
            m[v[l]]--;
            l++;
        }
        ans=min(ans,r-l+1);
    }
    r++;

 }
 if(ans==n+1)cout<<0<<endl;
 else cout<<ans<<endl;

 
}
signed main()
{
 optimize();
solve();

  
 
}
accuracy: 1
time_ms: 60
memory_kb: 12852
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef double dl;

#define PB push_back
#define F first
#define S second
#define MP make_pair
#define endl '\n'
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define sz(x) (ll) x.size()
#define mid(l, r) ((r + l) / 2)
#define left(node) (node * 2)
#define right(node) (node * 2 + 1)
#define mx_int_prime 999999937

const double PI = acos(-1);
const double eps = 1e-9;
const int infi = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007

#define mem(a, b) memset(a, b, sizeof(a))
#define gcd(a, b) __gcd(a, b)
#define sqr(a) ((a) * (a))

void solve()
{
    ll n, k;
    cin >> n >> k;

    vl v(n);
    for (ll i = 0; i < n; i++) cin >> v[i];


    ll l=0, r=0;
    ll ans = LLONG_MAX;
    set<ll>st;
    map<ll,ll>mp;

    while(r<n){
        if(v[r]<=k){
            mp[v[r]]++;
            st.insert(v[r]);
        }

        while(st.size()==k){
            ans = min(ans,(r-l)+1);
            if(v[l]<=k){
                mp[v[l]]--;
                if(mp[v[l]]==0){
                    st.erase(v[l]);
                }
            }
            l++;
        }

        r++;
    }
    if(ans == LLONG_MAX){
        cout << 0 << endl;
    }
    else{
        cout << ans << endl;
    }

}

int main()
{
    // ll t;
    // cin>>t;
    // while(t--){
    solve();
    // }
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 14728
final_rank: 17

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(){
    int n,k;cin>>n>>k;
    vector<int> a(n);
    for(auto &i:a)cin>>i;
    int r=0,ndis=0,ans=n+1;
    bool ok=1;
    map<int,int> freq;
    
    if(a[0]<=k && freq[a[0]]==0)ndis++;
    freq[a[0]]++;
    for(int i=0;i<n;i++){
        if(i>0){
            freq[a[i-1]]--;
            if(a[i-1]<=k && freq[a[i-1]]==0)ndis--;
        }
        while(ndis<k && r<n){
            r++;
            if(r<n && a[r]<=k && freq[a[r]]==0)ndis++;
            freq[a[r]]++;
        }
        if(ndis==k && r>=i){
            ans=min(ans,r-i+1);
            ok=0;
        }
    }

    if(ok)cout<<0<<'\n';
    else cout<<ans<<'\n';
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 8100
final_rank: 15

code_snippet: //! https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_3_B
#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, k;
    cin >> n >> k;

    vector<int> v(n), cnt(k + 1, 0), vis(k + 1, 0);
    for (auto &x : v) cin >> x;

    int currK = 0, ans = INT_MAX;
    queue<int> q;
    for (int i = 0; i < n; i++) {
        q.push(v[i]);
        if (v[i] <= k) {
            cnt[v[i]]++;

            if (vis[v[i]] == 0) currK++;
            vis[v[i]] = 1;
        }

        while (!q.empty() && (q.front() > k || cnt[q.front()] > 1)) {
            if (q.front() <= k) cnt[q.front()]--;
            q.pop();
        }

        if (currK == k) ans = min(ans, (int)(q.size()));
        // cout << "currK : " << currK << ' ' << q.size() << endl;
    }
    if (ans == INT_MAX) ans = 0;
    cout << ans << endl;

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4652
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main(){

int n, k;
cin >> n >> k;

int arr[n];

for(int i=0; i<n; i++) cin >> arr[i];

int ans = INT_MAX;

int flag = 0;

int l = 0, r = 0;

map<int,int>mp;

while(r<n){

if(arr[r]<=k) mp[arr[r]]++;

if(mp.size()==k){

flag = 1;  

while(mp.size()==k){

ans = min(ans, r-l+1);

if(arr[l]<=k) mp[arr[l]]--;

if(mp[arr[l]]==0) mp.erase(arr[l]);

l++;

}

}

r++;

}

cout<<(flag? ans : 0)<<'\n';

  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 8420
final_rank: 11

code_snippet: /**
         Author:  Md. Tanvir Ahmed rafi (Alraaafi)
         CF    : https://codeforces.com/profile/Alraaafi
**/
 
 /*
 
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█████████████████████░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░██████░░░░░░░░░░░░░░░░░░░███░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░██░░░░░░░░░░░░░░░░░░░░░░░░░░█░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░██░░░░░░░░░░█░░░░░░░░░░░░░░░░█░░░
███████████████████████████████████████░░░░░████████░░░░░░░
░░░░░░░░░░░░░████░░░░█░░░░░█░░░░░░░░░██░░░░░█░░░░░░████░░░░
░░░░░░░░░░░██░░░█░░░░██░░░░█░░░░░░░░██░░░░░░█░░░░░░░█░██░░░
░░░░░░░░███░░░░░█░░░░░█░░░░██░░░░░██░░░░░░░░█░░░░░░░████░░░
░░░░░░███░░░░░░░█░░░░░█░░░░░█░░░░░██████░░░░█░░░░░░░░░░░░░░
░░░░░░░░█████░░░█░░░░░█░░░░░██░░░░░░░██░░░░░█░░░░░░░░░░░░░░
░░░░░░░░░░░░░████░░░░░██░░░░░█░░░░████████████░░░░░░░░░░░░░
░░░░░░██░██░░░░░░░░░░░░█░░░░░█░░░░██░░░░░░░░░█░░░░░░░░░░░░░
░░░░░░█████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░█████╗░██╗░░░░░██████╗░░█████╗░░█████╗░░█████╗░███████╗██╗
██╔══██╗██║░░░░░██╔══██╗██╔══██╗██╔══██╗██╔══██╗██╔════╝██║
███████║██║░░░░░██████╔╝███████║███████║███████║█████╗░░██║
██╔══██║██║░░░░░██╔══██╗██╔══██║██╔══██║██╔══██║██╔══╝░░██║
██║░░██║███████╗██║░░██║██║░░██║██║░░██║██║░░██║██║░░░░░██║
╚═╝░░╚═╝╚══════╝╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░░░░░╚═╝
 
 
*/

 
#include<bits/stdc++.h>
using namespace std;

/*
//Policy based Data structure
//USE: ordered_set  st;
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
*/
 
 
//TYPEDEF
#define HA printf("YES\n")
#define NA printf("NO\n")
#define F first
#define S second

typedef long long  ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vpi;
typedef vector<pll> vpll;
typedef double dl;
#define testCase long long int TT,tc; cin>>TT; for(tc=1; tc<=TT; tc++)
#define output(a,n)    for(int i=0;i<n;i++) cout<<a[i]<<" ";  
#define endl "\n" //not for interactive Problem...
typedef priority_queue<ll> PQ;
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define POINT cout<<setprecision(10)<<fixed
#define all(a) (a).begin(),(a).end()
#define CASE cout<<"Case "<<tc<<": "
#define PB push_back
// Lamda Fn: auto fn_nm = [](var pera) { return expres; };

ll const md = 1e9 + 7;


int main(){

    //FAST;
    ll i,j;

    // testCase
    // {
    //     ll n,k;


    // }

    ll n,k;


    cin>>n>>k;

    ll a[n+4];

    for(i=1; i<=n; i++)
    {
        cin>>a[i];
    }

    map<ll,ll> mp;
    deque<ll> dq;

    ll jj;

    for(i=1; i<=n; i++)
    {
        if( a[i] <= k ) mp[ a[i] ]++;

        dq.push_back( a[i] );

        if( mp.size() == k ) 
        {
            jj = i;
            break;
        }
    }

    if( mp.size() < k )
    {
        cout<<0<<endl;
        return 0;
    }


    ll ans = dq.size();
    ll pre,post;
    //post = a[jj+1];

    for(i = jj+1;  ; )
    {
        
        
        pre = dq.front();

        dq.pop_front();
        if(pre<=k) mp[pre]--;

        if( ( i>n ) && (pre<=k) &&  (mp[pre]==0))
            break;

        //if( ( i>n) ) 
           // break;

        j = i;
        while( mp[pre] == 0 && ( pre<=k )  && ( i<=n )){
            if(i>n) break;
            //cout<<j <<" "<<a[j]<<endl;
            post = a[j];
            dq.push_back( post );
            if(( post<=k )) mp[post]++;
            j++;
            if( j> n+1 ) break;
        }
        i = j;

        //if( ( i>n) ) 
           // break;

        ll sz = dq.size();
        ans = min(ans, sz);


        if( i> n+1 ) break;

        //cout<<i<<" "<<sz<<" "<<pre<<" "<<post<<endl;
    }



    cout<<ans<<endl;

}
accuracy: 1
time_ms: 30
memory_kb: 11296
final_rank: 14

code_snippet: /**
         Author:  Md. Tanvir Ahmed rafi (Alraaafi)
         CF    : https://codeforces.com/profile/Alraaafi
**/
 
 /*
 
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█████████████████████░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░██████░░░░░░░░░░░░░░░░░░░███░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░██░░░░░░░░░░░░░░░░░░░░░░░░░░█░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░██░░░░░░░░░░█░░░░░░░░░░░░░░░░█░░░
███████████████████████████████████████░░░░░████████░░░░░░░
░░░░░░░░░░░░░████░░░░█░░░░░█░░░░░░░░░██░░░░░█░░░░░░████░░░░
░░░░░░░░░░░██░░░█░░░░██░░░░█░░░░░░░░██░░░░░░█░░░░░░░█░██░░░
░░░░░░░░███░░░░░█░░░░░█░░░░██░░░░░██░░░░░░░░█░░░░░░░████░░░
░░░░░░███░░░░░░░█░░░░░█░░░░░█░░░░░██████░░░░█░░░░░░░░░░░░░░
░░░░░░░░█████░░░█░░░░░█░░░░░██░░░░░░░██░░░░░█░░░░░░░░░░░░░░
░░░░░░░░░░░░░████░░░░░██░░░░░█░░░░████████████░░░░░░░░░░░░░
░░░░░░██░██░░░░░░░░░░░░█░░░░░█░░░░██░░░░░░░░░█░░░░░░░░░░░░░
░░░░░░█████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░█████╗░██╗░░░░░██████╗░░█████╗░░█████╗░░█████╗░███████╗██╗
██╔══██╗██║░░░░░██╔══██╗██╔══██╗██╔══██╗██╔══██╗██╔════╝██║
███████║██║░░░░░██████╔╝███████║███████║███████║█████╗░░██║
██╔══██║██║░░░░░██╔══██╗██╔══██║██╔══██║██╔══██║██╔══╝░░██║
██║░░██║███████╗██║░░██║██║░░██║██║░░██║██║░░██║██║░░░░░██║
╚═╝░░╚═╝╚══════╝╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░░░░░╚═╝
 
 
*/

 
#include<bits/stdc++.h>
using namespace std;

/*
//Policy based Data structure
//USE: ordered_set  st;
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
*/
 
 
//TYPEDEF
#define HA printf("YES\n")
#define NA printf("NO\n")
#define F first
#define S second

typedef long long  ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vpi;
typedef vector<pll> vpll;
typedef double dl;
#define testCase long long int TT,tc; cin>>TT; for(tc=1; tc<=TT; tc++)
#define output(a,n)    for(int i=0;i<n;i++) cout<<a[i]<<" ";  
#define endl "\n" //not for interactive Problem...
typedef priority_queue<ll> PQ;
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define POINT cout<<setprecision(10)<<fixed
#define all(a) (a).begin(),(a).end()
#define CASE cout<<"Case "<<tc<<": "
#define PB push_back
// Lamda Fn: auto fn_nm = [](var pera) { return expres; };

ll const md = 1e9 + 7;


int main(){

    //FAST;
    ll i,j;

    // testCase
    // {
    //     ll n,k;


    // }

    ll n,k;


    cin>>n>>k;

    ll a[n+4];

    for(i=1; i<=n; i++)
    {
        cin>>a[i];
    }

    map<ll,ll> mp;
    deque<ll> dq;

    ll jj;

    for(i=1; i<=n; i++)
    {
        if( a[i] <= k ) mp[ a[i] ]++;

        dq.push_back( a[i] );

        if( mp.size() == k ) 
        {
            jj = i;
            break;
        }
    }

    if( mp.size() < k )
    {
        cout<<0<<endl;
        return 0;
    }


    ll ans = dq.size();
    ll pre,post;
    //post = a[jj+1];

    for(i = jj+1;  ; )
    {
        
        
        pre = dq.front();

        dq.pop_front();
        if(pre<=k) mp[pre]--;

        if( ( i>n ) && (pre<=k) &&  (mp[pre]==0))
            break;

        //if( ( i>n) ) 
           // break;

        j = i;
        while( mp[pre] == 0 && ( pre<=k )  && ( i<=n )){
            if(i>n) break;
            //cout<<j <<" "<<a[j]<<endl;
            post = a[j];
            dq.push_back( post );
            if(( post<=k )) mp[post]++;
            j++;
        }
        i = j;

        //if( ( i>n) ) 
           // break;

        ll sz = dq.size();
        ans = min(ans, sz);

        //cout<<i<<" "<<sz<<" "<<pre<<" "<<post<<endl;
    }



    cout<<ans<<endl;

}
accuracy: 0.7391304347826086
time_ms: 20
memory_kb: 5492
final_rank: 19

code_snippet: /**
         Author:  Md. Tanvir Ahmed rafi (Alraaafi)
         CF    : https://codeforces.com/profile/Alraaafi
**/
 
 /*
 
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█████████████████████░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░██████░░░░░░░░░░░░░░░░░░░███░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░██░░░░░░░░░░░░░░░░░░░░░░░░░░█░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░██░░░░░░░░░░█░░░░░░░░░░░░░░░░█░░░
███████████████████████████████████████░░░░░████████░░░░░░░
░░░░░░░░░░░░░████░░░░█░░░░░█░░░░░░░░░██░░░░░█░░░░░░████░░░░
░░░░░░░░░░░██░░░█░░░░██░░░░█░░░░░░░░██░░░░░░█░░░░░░░█░██░░░
░░░░░░░░███░░░░░█░░░░░█░░░░██░░░░░██░░░░░░░░█░░░░░░░████░░░
░░░░░░███░░░░░░░█░░░░░█░░░░░█░░░░░██████░░░░█░░░░░░░░░░░░░░
░░░░░░░░█████░░░█░░░░░█░░░░░██░░░░░░░██░░░░░█░░░░░░░░░░░░░░
░░░░░░░░░░░░░████░░░░░██░░░░░█░░░░████████████░░░░░░░░░░░░░
░░░░░░██░██░░░░░░░░░░░░█░░░░░█░░░░██░░░░░░░░░█░░░░░░░░░░░░░
░░░░░░█████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░█████╗░██╗░░░░░██████╗░░█████╗░░█████╗░░█████╗░███████╗██╗
██╔══██╗██║░░░░░██╔══██╗██╔══██╗██╔══██╗██╔══██╗██╔════╝██║
███████║██║░░░░░██████╔╝███████║███████║███████║█████╗░░██║
██╔══██║██║░░░░░██╔══██╗██╔══██║██╔══██║██╔══██║██╔══╝░░██║
██║░░██║███████╗██║░░██║██║░░██║██║░░██║██║░░██║██║░░░░░██║
╚═╝░░╚═╝╚══════╝╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░░░░░╚═╝
 
 
*/

 
#include<bits/stdc++.h>
using namespace std;

/*
//Policy based Data structure
//USE: ordered_set  st;
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
*/
 
 
//TYPEDEF
#define HA printf("YES\n")
#define NA printf("NO\n")
#define F first
#define S second

typedef long long  ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vpi;
typedef vector<pll> vpll;
typedef double dl;
#define testCase long long int TT,tc; cin>>TT; for(tc=1; tc<=TT; tc++)
#define output(a,n)    for(int i=0;i<n;i++) cout<<a[i]<<" ";  
#define endl "\n" //not for interactive Problem...
typedef priority_queue<ll> PQ;
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define POINT cout<<setprecision(10)<<fixed
#define all(a) (a).begin(),(a).end()
#define CASE cout<<"Case "<<tc<<": "
#define PB push_back
// Lamda Fn: auto fn_nm = [](var pera) { return expres; };

ll const md = 1e9 + 7;


int main(){

    //FAST;
    ll i,j;

    // testCase
    // {
    //     ll n,k;


    // }

    ll n,k;


    cin>>n>>k;

    ll a[n+4];

    for(i=1; i<=n; i++)
    {
        cin>>a[i];
    }

    map<ll,ll> mp;
    deque<ll> dq;

    ll jj;

    for(i=1; i<=n; i++)
    {
        if( a[i] <= k ) mp[ a[i] ]++;

        dq.push_back( a[i] );

        if( mp.size() == k ) 
        {
            jj = i;
            break;
        }
    }

    if( mp.size() < k )
    {
        cout<<0<<endl;
        return 0;
    }


    ll ans = dq.size();
    ll pre,post;
    //post = a[jj+1];

    for(i = jj+1;  ; )
    {
        
        
        pre = dq.front();

        dq.pop_front();
        if(pre<=k) mp[pre]--;

        if( ( i>n ) && (pre<=k) &&  (mp[pre]==0))
            break;

        j = i;
        while( mp[pre] == 0 && ( pre<=k ) ){
            
            post = a[j];
            dq.push_back( post );
            if(( post<=k )) mp[post]++;
            j++;
        }
        i = j;


        ll sz = dq.size();
        ans = min(ans, sz);

        //cout<<i<<" "<<sz<<" "<<pre<<" "<<post<<endl;
    }



    cout<<ans<<endl;

}
accuracy: 0.7391304347826086
time_ms: 20
memory_kb: 5524
final_rank: 20

code_snippet: //  "Jai Shri Ram"
//  author : himu_mojumder
//  "Do more of what you enjoy most."

#include <bits/stdc++.h>
using namespace std;

// ---------------------- Macros ----------------------
#define upar(value) upper_bound(v.begin(), v.end(), value) - v.begin();
#define lower(value) lower_bound(v.begin(), v.end(), value) - v.begin();
#define endl "\n"
#define yes cout << "YES\n";
#define no cout << "NO\n";
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define gcd __gcd
#define pb push_back
#define ll long long
#define int long long

#define lop(i, a, b) for (int i = a; i < b; ++i)
#define rlop(i, a, b) for (int i = a; i >= b; --i)

#define input(v) for (int i = 0; i < n; ++i) cin >> (v)[i];

// ------------------ Sorting Macros ------------------
#define srt(v) sort(v.begin(), v.end());
#define rsrt(v) sort(v.rbegin(), v.rend());
#define rev(v) reverse(v.begin(), v.end());
#define uniq(v) srt(v); v.erase(unique(v.begin(), v.end()), v.end());

// ------------------ Typedefs ------------------
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef map<int, int> mii;

// ------------------ Custom Comparator ------------------
bool cmp(pair<int, int> &p1, pair<int, int> &p2)
{
    if ((p1.first - p1.second) == (p2.first - p2.second))
        return (p1.first < p2.first);
    return ((p1.first - p1.second) > (p2.first - p2.second));
    // je vave cai shebabe return korbo
}

// ------------------ Algorithm Macros ------------------
#define lcm(x, y) y * x / gcd(x, y)

// ------------------ Testcase Macro ------------------
#define test int tt; cin >> tt; while (tt--)

// ------------------ Solve Function ------------------
void solve()
{
    int n,k;
    cin>>n>>k;
    vector<int>v(n);
    for(int i=0;i<n;i++)cin>>v[i];
    map<int,int>mp;
    int i=0;
    int j=0;
    int ans=INT_MAX;
    while(j<n){
        if(v[j]<=k)
        mp[v[j]]++;
        if(mp.size()>=k){
            while(v[i]>k || mp[v[i]]>1){
                if(v[i]>k);
                else{
                    mp[v[i]]--;
                    if(mp[v[i]]==0){
                        mp.erase(mp[v[i]]);
                    }
                }
                i++;
            }
            int tem=j-i+1;
            ans=min(tem,ans);
        }
        j++;
    }
    if(ans==INT_MAX)cout<<0<<endl;
    else cout<<ans<<endl;
}
// ------------------ Main Function ------------------
signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
   // test
    solve();
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 10368
final_rank: 9

code_snippet: // linK: https://onlinejudge.u-aizu.ac.jp/problems/DSL_3_B
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef double dl;

#define endl "\n"
#define optimize()                \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define fraction()                \
    cout.unsetf(ios::floatfield); \
    cout.precision(10);           \
    cout.setf(ios::fixed, ios::floatfield);

void program()
{
    int n, k;
    cin >> n >> k;

    vector<int> v(n);

    for (int &vi : v)
        cin >> vi;

    map<int, int> mp;

    int l = 0;
    int r = 0;
    bool found = false;
    int ans = n;

    while (r < n)
    {
        if (v[r] <= k)
        {
            mp[v[r]]++;
        }

        if (mp.size() < k)
        {
            r++;
            continue;
        }

        while (mp.size() == k)
        {
            found = true;

            if (v[l] <= k)
            {
                mp[v[l]]--;
            }

            if (mp[v[l]] == 0)
            {
                mp.erase(v[l]);
            }

            int len = r - l + 1;

            ans = min(ans, len);
            l++;
        }
        r++;
    }

    cout << (found ? ans : found) << endl;
}

int main()
{
    optimize();
    program();
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 8076
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(){
        int n,k;
        cin>>n>>k;
        vector<int> v(n);
        for(int i=0;i<n;i++){

        	cin>>v[i];
        }


       int res=INT_MAX;
       int l=0;
       int c=0;
       map<int,int>m;


       for(int r=0;r<n;r++){
       	m[v[r]]++;
       	  if(v[r]>=1 && v[r]<=k && m[v[r]]==1){
       	  	c++;
       	  }

       	  while(c==k){
       	  	res=min(res,r-l+1);
       	  	m[v[l]]--;
       	  	 if(v[l]>=1 && v[l]<=k && m[v[l]]==0){
       	  	c--;
       	  }
       	  l++;

       	  }


       }
       if(res==INT_MAX){
       	cout<<0<<endl;

       }
       else
       	cout<<res<<endl;







	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 8132
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int &x : a) cin >> x;

    map<int, int> freq;
    int have = 0, l = 0, r = 0, res = n + 1;

    while (r < n) {
        if (a[r] >= 1 && a[r] <= k) {
            freq[a[r]]++;
            if (freq[a[r]] == 1) have++;
        }
        while (have == k) {
            res = min(res, r - l + 1);
            if (a[l] >= 1 && a[l] <= k) {
                freq[a[l]]--;
                if (freq[a[l]] == 0) have--;
            }
            l++;
        }
        r++;
    }
    if (res == n + 1) cout << 0 << endl;
    else cout << res << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 8048
final_rank: 7

code_snippet: // Bismillahir Rahmanir Raheem



#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef double dl;


#define endl '\n'
#define mx_int_prime 999999937
#define tc int t;cin>>t;while(t--)
#define   yes             cout<<"YES"<<endl;
#define   no              cout<<"NO"<<endl;
#define   fr(i,a,n)        for(int i=a;i<n;i++)





#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);
#define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);




void solve(){
   int n,k;
   cin>>n>>k;
  vi v(n);
  for(int i=0;i<n;i++){
    cin>>v[i];
  }
  int l=0,ans=INT_MAX,cnt=0;
  map<int,int>mp;
   for(int r=0;r<n;r++){
      if(v[r]>=1&&v[r]<=k){
            if(mp[v[r]] == 0) cnt++;
         mp[v[r]]++;

      }
      while(cnt==k){
        ans=min(ans,(r-l+1));

      if(v[l]>=1&&v[l]<=k){
         mp[v[l]]--;;
         if(mp[v[l]] == 0) cnt--;
      }
        l++;
      }
   }

 if(ans==INT_MAX)cout<<0<<endl;
 else cout<<ans<<endl;

  return ;


    }



int main()
{
    optimize();




            solve();


    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 8032
final_rank: 6

code_snippet: #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;


#define ll          long long
#define double      long double
#define faster      ios_base::sync_with_stdio(0);
#define PI          acos(-1.0)
#define all(x)      x.begin(),x.end()
#define rall(x)     x.rbegin(),x.rend()
#define endl        "\n"
#define PrintCase   cout<<"Case "<<cs<<": "
#define fraction(x) fixed<<setprecision(x)
#define pinf        INT_MAX
#define minf        INT_MIN

//Debug
#define DebugSingle(x)      for(auto h: x){cout<<h<<" ";}cout<<endl;
#define DebugPair(x)        for(auto h: x){cout<<h.first<<" "<<h.second<<endl;}

#define vi      vector<int>
#define vvi     vector<vi>
#define vb      vector<bool>
#define pii     pair<int, int>
#define vpii    vector<pii>
#define vvpii   vector<vpii>

//input an array
#define fin(a)     for(auto &h: a){cin>> h;}


void solve(){
    int n, k;
    cin>> n >> k;
    vector<int> a(n);
    set<int> st;
    for(int i = 0; i < n; i++){
        cin>> a[i];
        if(a[i] <= k)
            st.insert(a[i]);
    }
    if(st.size() < k){
        cout<< 0 <<endl;
        return;
    }

    map<int, int> mp;
    int ans = INT_MAX, j = 0;
    for(int i = 0; i < n; i++){
        if(a[i] <= k){
            mp[a[i]]++;
        }
        while(mp.size() == k){
            if(a[j] <= k){
                mp[a[j]]--;
                if(mp[a[j]] == 0){
                    mp.erase(a[j]);
                }
            }
            ans = min(ans, i - j + 1);
            j++;
            if(j >= n){
                break;
            }
        }
    }
    cout<< ans <<endl;



}



int main()
{
    faster
    solve();
    
}
accuracy: 1
time_ms: 40
memory_kb: 12856
final_rank: 16

code_snippet: #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;


#define ll          long long
#define double      long double
#define faster      ios_base::sync_with_stdio(0);
#define PI          acos(-1.0)
#define all(x)      x.begin(),x.end()
#define rall(x)     x.rbegin(),x.rend()
#define endl        "\n"
#define PrintCase   cout<<"Case "<<cs<<": "
#define fraction(x) fixed<<setprecision(x)
#define pinf        INT_MAX
#define minf        INT_MIN

//Debug
#define DebugSingle(x)      for(auto h: x){cout<<h<<" ";}cout<<endl;
#define DebugPair(x)        for(auto h: x){cout<<h.first<<" "<<h.second<<endl;}

#define vi      vector<int>
#define vvi     vector<vi>
#define vb      vector<bool>
#define pii     pair<int, int>
#define vpii    vector<pii>
#define vvpii   vector<vpii>

//input an array
#define fin(a)     for(auto &h: a){cin>> h;}


void solve(){
    ll n, k;
    cin>> n >> k;
    vector<ll> a(n);
    fin(a);
    map<int, int> mp;
    int id = -1;
    for(int i = 0; i < n; i++){
        if(a[i] <= k){
            mp[a[i]]++;
        }

        if(mp.size() == k){
            id = i;
            break;
        }
    }

    if(id == -1){
        cout<< 0 <<endl;
        return;
    }

    int j = id, ans = id + 1;

    for(int i = 0; i < n; i++){
        if(a[i] <= k and mp[a[i]] == 1){
            while(1){
                j++;
                if(j >= n){
                    break;
                }
                if(a[j] <= k){
                    mp[a[j]]++;
                }
                if(a[i] == a[j]){
                    break;
                }
            }
        }
        if(a[i] <= k){
            mp[a[i]]--;
            if(mp[a[i]] == 0){
                break;
            }
        }
        if(j < n)
            ans = min(ans, j - i);
    }
    cout<< ans <<endl;
    



}



int main()
{
    faster
    solve();
    
}
accuracy: 1
time_ms: 20
memory_kb: 8632
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here 
	int t,n,i,j,k,g,h,st,f;
	vector<int> v;
	map<int,int> m;
	cin>>n>>k;
	for(i=0;i<n;i++)
	{
		cin>>g;
		v.push_back(g);
	}
	h=INT_MAX;
	st=0;
	for(i=0;i<n;i++)
	{
		if(v[i]>k)
		{
			continue;
		}
		else 
		{
			m[v[i]]++;
			while(st<i && m.size()==k && (v[st]>k || m[v[st]]>1))
			{
				if(v[st]<=k)
				m[v[st]]--;
				st++;
			}
			if(m.size()==k)
			{
				if(h>i-st+1)
				h=i-st+1;
			}
		}
	}
	if(h==INT_MAX)
	{
		cout<<"0\n";
	}
	else 
	{
		cout<<h<<"\n";
	}
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 8112
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int n,k; cin>>n>>k;
	int arr[n];
	map<int,int> mp;
	int e=0,f=0,min=n+1;
	for(e=0;e<n;e++){
		cin>>arr[e];
		if(arr[e]<=k && arr[e]>=1){
			mp[arr[e]]++;
		}
		if(mp.size()==k){
			if(min>e-f+1) min=e-f+1;
		}
		while(f<=e && mp.size()==k){
			if(mp.count(arr[f])){
				mp[arr[f]]--;
				if(mp[arr[f]]==0) mp.erase(arr[f]);
			}
			f++;
			if(mp.size()==k){
				if(min>e-f+1) min=e-f+1;
			}
		}
	}
	if(min==n+1) cout<<0<<"\n";
	else cout<<min<<"\n";
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 8400
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define ll          long long
#define double      long double
#define faster      ios_base::sync_with_stdio(0);
#define PI          acos(-1.0)
#define all(x)      x.begin(),x.end()
#define rall(x)     x.rbegin(),x.rend()
#define endl        "\n"
#define PrintCase   cout<<"Case "<<cs<<": "
#define fraction(x) fixed<<setprecision(x)
#define pinf        INT_MAX
#define minf        INT_MIN

//Debug
#define DebugSingle(x)      for(auto h: x){cout<<h<<" ";}cout<<endl;
#define DebugPair(x)        for(auto h: x){cout<<h.first<<" "<<h.second<<endl;}

#define vi      vector<int>
#define vvi     vector<vi>
#define vb      vector<bool>
#define pii     pair<int, int>
#define vpii    vector<pii>
#define vvpii   vector<vpii>

//input an array
#define fin(a)     for(auto &h: a){cin>> h;}


void solve(){
    int n, k;
    cin>> n >> k;
    vector<int> a(n);
    fin(a);
    map<int, int> mp;
    int id = -1;
    for(int i = 0; i < n; i++){
        if(a[i] <= k){
            mp[a[i]]++;
        }
        if(mp.size() == k){
            id = i;
            break;
        }
    }
    // cout<< id <<endl;
    if(id == -1){
        cout<< 0 <<endl;
        return;
    }

    int ans = id + 1, j = id;
    for(int i = 0; i < n; i++){
        if(a[i] <= k and mp[a[i]] == 1){
            while(1){
                j++;
                if(j >= n){
                    break;
                }
                if(a[j] <= k){
                    mp[a[j]]++;
                }
                if(a[j] == a[i]){
                    break;
                } 
            }
        }
        if(a[i] <= k){
            mp[a[i]]--;
            if(mp[a[i]] == 0){
                break;
            }
        }
        if(j < n){
            ans = min(ans, j - i);
        }
        // cout<< j << " " << i <<endl;
    }
    cout<< ans <<endl;



}



int main()
{
    faster
    solve();
    
}
accuracy: 1
time_ms: 20
memory_kb: 8100
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(){
	long long n,k;
	cin>>n>>k;
	map<long long,long long>cnt;
	deque<long long>x;
	long long mn=1e18;
	for(long long i=0;i<n;i++){
		long long c;
		cin>>c;
		if(c<k+1){
			cnt[c]+=1;
		}
		x.push_back(c);
		if(cnt.size()==k){
			while(1){
				long long v=x.front();
				if(v<k+1&&cnt[v]!=1){
					x.pop_front();
					cnt[v]-=1;
				}
				else if(v<k+1&&cnt[v]==1){
			
					break;
				}
				else{
					x.pop_front();
				}
			}
			mn=min(mn,(long long)x.size());
		}
		

	}
	if(cnt.size()==k){
		cout<<mn<<endl;
	}
	else{
		
		cout<<"0"<<endl;
	}

}
accuracy: 1
time_ms: 30
memory_kb: 10508
final_rank: 13

code_snippet: /*********************** In the Name of ALLAH ************************/
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define fl(n) for(ll i = 0; i < n;i++)
#define flj(n) for(ll j = 0; j < n;j++)
#define fastio ios_base::sync_with_stdio(false);
#define ll long long
#define endl "\n"
using namespace std;
using namespace __gnu_pbds;
#define MOD 1000000007
template<typename T> using ordered_set = tree < T, null_type,less<T>, rb_tree_tag, tree_order_statistics_node_update>;

//Comparator
//bool cmp(const pair<ll,ll>&a,const pair<ll,ll>&b)
//{
//    if(a.first < b.first) return 1;
//    else if(a.first == b.first) return (a.second > b.second);
//    return 0;
//}


int main()
{
    fastio;
    int n,k;
    cin >> n >> k;
    int arr[n];
    fl(n)
    {
        cin >> arr[i];
    }
    map<int,int>mp;
    deque<int>dq;
    queue<int>q;
    for(auto i : arr) q.push(i);
    bool flag = false;
    fl(n)
    {
        if(arr[i] >= 1 && arr[i] <= k)
        {
            mp[arr[i]]++;
        }
        dq.push_back(arr[i]);
        q.pop();
        if(mp.size() == k)
        {
            flag = true;
            break;
        }
    }
    if(!flag)
    {
        cout << 0 << endl;
        return 0;
    }
    int mini = dq.size();
    while(true)
    {
        mini = min((int)dq.size(),mini);
        int x = dq.front();
        if(mp[x] == 1)
        {
            while(!q.empty())
            {
                int y = q.front();
                dq.push_back(y);
                if(y >= 1 && y <= k) mp[y]++;
                q.pop();
                if(mp[x] > 1) break;
            }
        }
        dq.pop_front();
        if(x >= 1 && x <= k)
        {
            mp[x]--;
            if(mp[x] < 1) break;
        }
    }
    cout << mini << endl;

}
accuracy: 1
time_ms: 20
memory_kb: 8904
final_rank: 5