265. Count subarrays with given sum

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

Example 1:

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

Output: 2

Explanation: In the given array [1, 1, 1], there are two subarrays that sum up to 2: [1, 1] and [1, 1]. Hence, the output is 2.

Example 2:

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

Output: 2

Explanation: In the given array [1, 2, 3], there are two subarrays that sum up to 3: [1, 2] and [3]. Hence, the output is 2.

Now Your Turn!

Pick the correct output for the given input

Input: nums = [3, 1, 2, 4], k = 6

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  •    1 <= nums.length <= 105
  •    -1000 <= nums[i] <= 1000
  •    -107 <= k <= 107

Fun Facts

0
class Solution{
public:
int subarraySum(vector<int> &nums, int k){
}
};
Test Case

Input:

K
Nums