Description

Given an array of integers and an integer k, return the total number of continuous subarrays whose sum equals k.

Examples

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

Subarrays [1,1] at indices 0-1 and 1-2.

Input:nums = [3,-3,1,1,1], k = 3
Output:3
Explanation:

Three subarrays sum to 3: [3] at index 0 (3=3), [3,-3,1,1,1] from indices 0-4 (3-3+1+1+1=3), and [1,1,1] from indices 2-4 (1+1+1=3). The negative number -3 allows the full array to also sum to k despite containing more elements.

Input:nums = [1], k = 1
Output:1
Explanation:

Only one subarray [1] equals k=1. This tests the edge case of a single-element array where the element equals the target sum.

Constraints

  • 1 ≤ nums.length ≤ 2 * 10⁴
  • -1000 ≤ nums[i] ≤ 1000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!