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 maxlen=0;
  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(sum-k)!=mpp.end()){
  17. int len=i-mpp[sum-k];
  18. maxlen=max(maxlen,len);
  19. }
  20. if(mpp.find(sum)==mpp.end())mpp[sum]=i;
  21. }
  22. cout<<maxlen;
  23. return 0;
  24. }
Success #stdin #stdout 0.01s 5308KB
stdin
Standard input is empty
stdout
5