This question evaluates a candidate's competency in array algorithms and reasoning about contiguous subarray sums, including correct handling of negative and zero values.
Given an integer array nums (may contain negative numbers and zeros) and an integer k, return the number of contiguous subarrays whose sum is exactly k.
nums: int[]
,
k: int
count: int
(number of contiguous subarrays with sum
k
)
nums = [1, 2, 3], k = 3
→ output
2
because subarrays
[1,2]
and
[3]
sum to 3.