fork download
  1. #include <iostream>
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5. int main() {
  6. // your code goes here
  7. vector<int>arr={1,0,1,1,0,2};
  8. int k=3;
  9. unordered_map<int,int>mpp;
  10. mpp[0]=-1;
  11. int minlen=INT_MAX;
  12. int sum=0;
  13. for(int i=0;i<arr.size();i++){
  14. sum+=arr[i];
  15. int rem=sum-k;
  16. if(mpp.find(rem)!=mpp.end()){
  17. int len=i-mpp[rem];
  18. minlen=min(minlen,len);
  19. }
  20. mpp[sum]=i;
  21. }
  22. if(minlen == INT_MAX)
  23. cout << "No subarray found";
  24. else
  25. cout << minlen;
  26. return 0;
  27. }
Success #stdin #stdout 0.01s 5308KB
stdin
Standard input is empty
stdout
3