Subarray Sum Equals K

Given an integer array nums and an integer k, return the number of non-empty contiguous subarrays whose elements sum to k.

Given an integer array `nums` and an integer `k`, return the number of non-empty contiguous subarrays whose elements sum to `k`.

- `1 <= nums.length <= 2 * 10^4` - `-1000 <= nums[i] <= 1000` - `-10^7 <= k <= 10^7` - The interactive visualizer accepts at most 12 values with targets from -10,000 through 10,000; larger valid inputs are execution-only.

Example 1: input nums = [1,1,1], k = 2; output 2. The subarrays covering indices [0..1] and [1..2] both sum to 2.

Example 2: input nums = [1,2,3], k = 3; output 2. The subarrays [1,2] and [3] both sum to 3.

Category: Arrays & Hashing. Topic: arrays. Difficulty: Medium. Patterns: hashing, prefix-sum.