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