Subarray Sum Equals K

Given an array nums and a number k, return how many non-empty runs of consecutive numbers (subarrays) add up to exactly k. The numbers can be negative, so a sliding window alone won't do.

Constraints

  • 1 ≤ length of nums ≤ 20,000
  • -1,000 ≤ nums[i] ≤ 1,000
  • -10,000,000 ≤ k ≤ 10,000,000

Example 1

Input
nums = [1,1,1]
k = 2
Output
2
Why:

Positions 0 to 1 and 1 to 2.

Example 2

Input
nums = [1,2,3]
k = 3
Output
2
Why:

[1, 2] and [3].

Hints

    Was this a good challenge?

    Solved by
    0
    Submissions
    0
    Accepted
    –