-
Notifications
You must be signed in to change notification settings - Fork 0
/
DominatedSubarray.cpp
48 lines (47 loc) · 1.01 KB
/
DominatedSubarray.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <bits/stdc++.h>
#define ll long long int
#define endl "\n"
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define MAX_SIZE 1000005
using namespace std;
int main()
{
fast
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int arr[n];
vector<int> index[n+1];
vector<int>::iterator itr;
for(int i=0;i<n;++i)
{
cin>>arr[i];
int a=arr[i];
index[a].push_back(i);
}
if(n<2)
{
cout<<"-1"<<endl;
continue;
}
int count=INT_MAX;
for(int i=0;i<n;++i)
{
if(count==2)
break;
int temp;
itr= upper_bound(index[arr[i]].begin(),index[arr[i]].end(), i);
if(itr!=index[arr[i]].end() && *itr-i+1<count)
count=*itr-i+1;
}
if(count==INT_MAX)
{
cout<<"-1"<<endl;
continue;
}
cout<<count<<endl;
}
}