Simple example
Input
nums = [1, 1, 1], k = 2
Output
2
Subarrays [1,1] at positions (0,1) and (1,2) each sum to 2.
Full lesson preview
Count the number of contiguous subarrays whose elements sum to a target k using an efficient approach.
Problem statement
Task
Examples
Input
nums = [1, 1, 1], k = 2
Output
2
Subarrays [1,1] at positions (0,1) and (1,2) each sum to 2.
Input format
Output format
Constraints
Samples
Input
nums = [1, -1, 0], k = 0
Output
3
Subarrays [1,-1], [0], and [1,-1,0] sum to 0.