fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int getSubSum(int n,vector<int>&a,int k){
  4. int count=0;
  5. for(int i=0;i<n;i++){
  6. int sum=0;
  7. for(int j=i;j<n;j++){
  8. sum+=a[j];
  9. if(sum>=k){
  10. count++;
  11. }
  12. }
  13. }
  14. return count;
  15. }
  16.  
  17. int main() {
  18. // your code goes here
  19. int n;
  20. cin>>n;
  21. vector<int>a(n,0);
  22. for(int i=0;i<n;i++){
  23. cin>>a[i];
  24. }
  25. int k;
  26. cin>>k;
  27. cout<<"The count of subarrays with sum>=k"<<getSubSum(n,a,k);
  28. return 0;
  29. }
Success #stdin #stdout 0.01s 5320KB
stdin
4
1 2 3 4
3
stdout
The count of subarrays with sum>=k8