fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5.  
  6. void solve(){
  7. int n;
  8. cin>>n;
  9. vector<int>v(n);
  10. for(int &i:v)cin>>i;
  11. int k;
  12. cin>>k;
  13. unordered_map<int,int>mp;
  14. int mn=INT_MAX;
  15. for(int i=0;i<n;i++){
  16. int start= abs(v[i]-k);
  17. if(mp.count(start)){
  18. mn=min(mn,i-mp[start]+1);
  19. }
  20. if(!mp.count(v[i]))mp[v[i]]=i;
  21. }
  22. if(mn==INT_MAX)cout<<-1<<"\n";
  23. else cout<<mn<<"\n";
  24. }
  25.  
  26. int main(){
  27. ios_base::sync_with_stdio(false);
  28. cin.tie(NULL);
  29. solve();
  30. return 0;
  31. }
Success #stdin #stdout 0.01s 5328KB
stdin
Standard input is empty
stdout
-1