268. Subarray Sum Equals K
MediumPrefix Sum
Given an integer array `nums` and an integer `k`, return the total number of subarrays whose elements sum to `k`. A subarray is a contiguous non-empty sequence of elements within the array. For example, given nums = [1,1,1] and k = 2, there are two subarrays that sum to 2: nums[0..1] = [1,1] and nums[1..2] = [1,1], so the answer is 2.
Examples
Input: [1,1,1] 2
Output: 2
Explanation: Two subarrays sum to 2: nums[0..1] = [1,1] and nums[1..2] = [1,1].
Constraints
- 1 <= nums.length <= 2 * 10^4
- -1000 <= nums[i] <= 1000
- -10^7 <= k <= 10^7
Loading...
Run checks all cases above. Submit evaluates all test cases.