Untitled

 avatar
user_5668965
c_cpp
15 days ago
1.1 kB
1
Indexable
Never
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fast                       \
 ios_base::sync_with_stdio(0);      \
 cin.tie(0);                         \
 cout.tie(0);

int main(){
    fast;
    ll t;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;
        vector<ll> a(n);
        for(ll i=0;i<n;i++) cin>>a[i];
        vector<ll> b(n+1,0);
        for(ll i=0;i<n;i++){
            if(a[i]<=n) b[a[i]]++;
        }
        ll mex=0;
        for(ll i=0;i<=n;i++){
            if(b[i]==0){
                mex=i;
                break;
            }
        }
        ll ans=n,l=0;
        vector<ll> v(mex,0);
        ll cnt=0;
        for(ll i=0;i<n;i++){
            if(a[i]<mex){
                if(v[a[i]]==0) cnt++;
                v[a[i]]++;
            }
            while(l<=i && cnt==mex){
                ans=min(ans,(i-l+1));
                if(a[l]<mex){
                    if(v[a[l]]==1) cnt--;
                    v[a[l]]--;
                }
                l++;
            }
        }
        cout<<ans<<"\n";
    }
}
Leave a Comment