| 1 | #include <vector> |
| 2 | |
| 3 | int kSumSubarrays(std::vector<int>& nums, int k) { |
| 4 | int n = nums.size(); |
| 5 | int count = 0; |
| 6 | // Populate the prefix sum array, setting its first element to 0. |
| 7 | std::vector<int> prefixSum; |
| 8 | prefixSum.push_back(0); |
| 9 | for (int i = 0; i < n; i++) { |
| 10 | prefixSum.push_back(prefixSum.back() + nums[i]); |
| 11 | } |
| 12 | // Loop through all valid pairs of prefix sum values to find all |
| 13 | // subarrays that sum to 'k'. |
| 14 | for (int j = 1; j <= n; j++) { |
| 15 | for (int i = 1; i <= j; i++) { |
| 16 | if (prefixSum[j] - prefixSum[i - 1] == k) { |
| 17 | count += 1; |
| 18 | } |
| 19 | } |
| 20 | } |
| 21 | return count; |
| 22 | } |
nothing calls this directly
no outgoing calls
no test coverage detected