fork download
  1. #include <iostream>
  2. using namespace std;
  3. #include <bits/stdc++.h>
  4.  
  5. int main() {
  6. // your code goes here
  7.  
  8. vector<int>arr={1,2,1,0,1,1,0};
  9. int n=arr.size();
  10.  
  11. int k;
  12. cin>>k;
  13. int window_sum=0;
  14. int i=0,j=0;
  15. int max_length=INT_MIN;
  16. while(j<n)
  17. {
  18. window_sum+=arr[j];
  19.  
  20. while(i<=j && window_sum<=k)
  21. {
  22. max_length=max(max_length,j-i+1);
  23. window_sum=window_sum-arr[i];
  24. i++;
  25. }
  26. j++;
  27. }
  28. if(max_length==INT_MIN)
  29. {
  30. cout<<-1;
  31. }
  32. else
  33. {
  34. cout<<max_length;
  35. }
  36. // cout<<max_length;
  37.  
  38.  
  39. return 0;
  40. }
Success #stdin #stdout 0.01s 5280KB
stdin
4
stdout
1