fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int getSmall(vector<int> &arr,int L,int R){
  4. int mini=1e9;
  5. int n=arr.size();
  6. for(int i=0;i<n;i++){
  7. unordered_map<int,int>mp;
  8. for(int j=i;j<n;j++){
  9. if(arr[j]>=L && arr[j]<=R){
  10. mp[arr[j]]=mp[arr[j]]+1;
  11. }
  12. if(mp.size()==abs(R-L+1)){
  13. int len=j-i+1;
  14. mini=min(len,mini);
  15. }
  16.  
  17. }
  18. }
  19. return mini;
  20. }
  21.  
  22. int main() {
  23. // your code goes here
  24. int n;
  25. vector<int>v(n);
  26. for(int i=0;i<n;i++){
  27. cin>>v[i];
  28. }
  29. int L;
  30. cin>>L;
  31. int R;
  32. cin>>R;
  33. cout<<"Smallest subarray containing all the numbers from the range L to R is:"<<getSmall(v,L,R);
  34. return 0;
  35. }
Success #stdin #stdout 0.01s 5316KB
stdin
5
5 2 4 3 1
2 3
stdout
Smallest subarray containing all the numbers from the range L to R is:1000000000