Given an array of integers and a target sum, design an algorithm to count the number of subarrays with a sum equal to the target sum. A subarray is a contiguous subset of the array.
Explanation: The subarrays with sum 5 are [2, 3] and [5].
Explanation: The subarrays with sum 0 are [-1, 0, 1], [-1, -1, 2] and [-1, -4].
Explanation: The subarrays with sum 0 are [0], [0], [0], [0, 0], [0, 0], and [0, 0, 0].
Count subarrays with given sum using prefix
No dry run loaded.
🚀 Practice this problem
Run code, get AI hints & track streak