Given an array of integers nums and an integer k, return the total number of contiguous, non-empty subarrays whose elements sum to exactly k.
A subarray is a contiguous, non-empty sequence of elements within the array. Two subarrays are considered different if they start or end at different indices, even when they contain equal values.
Examples
Example 1
Input: nums = [1,1,1], k = 2
Output: 2
Explanation: The subarrays nums[0..1] and nums[1..2] each sum to 2.
Example 2
Input: nums = [1,2,3], k = 3
Output: 2
Explanation: The subarray nums[0..1] sums to 3, and the single element nums[2] sums to 3.
Constraints
1≤nums.length≤2×104
−1000≤nums[i]≤1000
−107≤k≤107
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up