64. Subarray Sum Equals K

MediumArrayArray

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

Input: An integer array nums and an integer k.

Output: Integer — count of subarrays with sum equal to k.

Examples

Example 1
Input: [1,1,1],2
Output: 2
Explanation: Subarrays with sum 2: [1,1] at indices [0,1] and [1,2]. Count=2.
Example 2
Input: [1,2,3],3
Output: 2
Explanation: Subarrays: [3] at index 2 and [1,2] at indices [0,1]. Count=2.
Example 3
Input: [1,-1,1,-1,1],0
Output: 4
Explanation: Subarrays summing to 0: [1,-1],[1,-1,1,-1],[-1,1],[1,-1,1,-1,1]. Count=4.

Constraints

Asked by

MetaAccentureInfosysMicrosoftAmazonBloomberg
Solve this problem in the editor →