fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. //Atleast K
  7. int n=5;
  8. int a[] = {1 , 11 , 2 , 3 , 15};
  9. int k = 10;
  10.  
  11. int cnt = 0;
  12. int sum = 0;
  13.  
  14. int i=0 , j=0;
  15. while(j<n)
  16. {
  17. // do calculation
  18. sum = sum + a[j]; // Running sum find out kela
  19.  
  20. if(sum < k)
  21. {
  22. j++; // we will simple increment ' j ' pointer
  23. }
  24. else if(sum>=k)
  25. {
  26. while(i<=j && sum>=k)
  27. {
  28. // we will find ans <- from the calculation
  29. cnt = cnt + (n-j);
  30.  
  31. sum = sum - a[i]; // from left of the window we will remove the element 'i' index element
  32.  
  33. i++; // after removing that element we will do i++
  34. }
  35. j++; // and we will also do j++
  36. }
  37. }
  38. cout<<cnt<<" ";
  39. }
Success #stdin #stdout 0s 5308KB
stdin
Standard input is empty
stdout
11