fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. int n,l,r;
  7. cin>>n>>l>>r;
  8. vector<int> a(n);
  9. for(int i=0;i<n;i++){
  10. cin>>a[i];
  11. }
  12. int i=0,j=0;
  13. int k=r-l+1;
  14. int minWin=n;
  15. int minInd=-1;
  16. unordered_map<int,int> mp;
  17. while(j<n){
  18. if(a[j]>=l&&a[j]<=r){
  19. mp[a[j]]++;
  20. }
  21. if(mp.size()==k){
  22. while(mp.size()==k){
  23. if(j-i+1<minWin){
  24. minWin = j-i+1;
  25. minInd= i;
  26. }
  27. if(mp.find(a[i])!=mp.end()){
  28. mp[a[i]]--;
  29. if(mp[a[i]]==0){
  30. mp.erase(a[i]);
  31. }
  32. }
  33. i++;
  34. }
  35. }
  36. j++;
  37. }
  38. if(minInd==-1){
  39. cout<<-1;
  40. }else{
  41. cout<<minInd+1; //1 based indexing
  42. }
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0s 5292KB
stdin
8 2 4
2 1 4 3 2 1 1 4
stdout
3