Given an integer array and an integer k, return the number of continuous subarrays where the sum of the elements equals k.
Explanation: Subarrays with sum 8 are [4, 4] (does not exist in this array), [1, 7], [7, 1], and [2, 5, 1] (does not exist in this array) does not meet the continuous subarray requirement but [1, 7] and [2, 5, 1] meet the requirement, [7, 1] meet the requirement
Explanation: Subarrays with sum 7 are [3, 1, 2, 1] does not meet the requirement (it has extra 1 that does not exist) but [6, 1], [2, 5] meet the requirement
Explanation: Subarray with sum 15 is [9, 6] does not meet the requirement (it has 6 that does not exist) but [7, 5, 3] meet the requirement
Count subarrays with given sum using prefix
No dry run loaded.
🚀 Practice this problem
Run code, get AI hints & track streak