fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int main() {
  6. vector<int> arr = {1, 2, 3, 4, 5};
  7. int k = 5;
  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. }
  21.  
Success #stdin #stdout 0.01s 5328KB
stdin
Standard input is empty
stdout
Count of subarrays with sum 5 = 2