fork download
  1. // TC : O(N^2)
  2. // SC : O(N)
  3.  
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. int main() {
  8. int n;
  9. cin >> n;
  10.  
  11. int arr[n];
  12. for(int i=0; i<n; i++) {
  13. cin >> arr[i];
  14. }
  15.  
  16. int count = 0;
  17. int k;
  18. cin >> k;
  19.  
  20. for(int i=0; i<n; i++) { // O(N)
  21. int sum = 0;
  22. for(int j=i; j<n; j++) { // O(N)
  23. sum += arr[j];
  24. if(sum <= k) {
  25. count++;
  26. }
  27. }
  28. }
  29. cout << "Count : " << count;
  30. return 0;
  31. }
Success #stdin #stdout 0.01s 5328KB
stdin
5
1
2
3
5
8
4
stdout
Count : 4