fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int main() {
  6. vector<int> arr = {10, 2, -2, -20, 10};
  7. int k = -10;
  8. int count = 0;
  9.  
  10. for(int i = 0; i < arr.size(); i++) {
  11. int sum = 0;
  12. for(int j = i; j < arr.size(); j++) {
  13. sum += arr[j];
  14. if(sum == k) count++;
  15. }
  16. }
  17.  
  18. cout << "Count of subarrays with sum " << k << " = " << count << endl;
  19. return 0;
  20. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Count of subarrays with sum -10 = 3