medium

Subarray Sum Equals K

Given an integer array (which may contain negatives) and an integer k, count the number of contiguous subarrays whose elements sum to exactly k.

Constraints
  • 1 ≤ n ≤ 2 · 10^4
  • -1000 ≤ nums[i] ≤ 1000
  • -10^7 ≤ k ≤ 10^7
Examples
in: nums = [1,1,1], k = 2
out: 2
in: nums = [1,2,3], k = 3
out: 2

Code it yourself

Solve in
Test execution is not yet available for this exercise.Practice journal →Draft saved in this browser.
Hints:
Which approach applies?

Choose an approach to check your pattern recognition, or reveal the discussion when you need help.