fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int getCount(int n,vector<int>v,int k){
  4. int count=0;
  5. for(int i=0;i<n;i++){
  6. int sum=0;
  7. for(int j=0;j<n;j++){
  8. sum+=v[j];
  9. if(sum<=k){
  10. count+=1;
  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>v(n,0);
  22. for(int i=0;i<n;i++){
  23. cin>>v[i];
  24. }
  25. int k;
  26. cin>>k;
  27. cout<<"The number of subarray with sum less than k is:"<<getCount(n,v,k);
  28. return 0;
  29. }
Success #stdin #stdout 0s 5320KB
stdin
5
2 1 1 5 8
4
stdout
The number of subarray with sum less than k is:15