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

A subarray is a contiguous non-empty sequence of elements within an array.

&nbsp;
Example 1:
Input: nums = [1,1,1], k = 2
Output: 2
Example 2:
Input: nums = [1,2,3], k = 3
Output: 2

&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 2 * 104
	-1000 &lt;= nums[i] &lt;= 1000
	-107 &lt;= k &lt;= 107


class Solution:
	def subarraySum(self, nums: List[int], k: int) -> int:

		ans=0
		prefsum=0
		d={0:1}

		for num in nums:
			prefsum = prefsum + num

			if prefsum-k in d:
				ans = ans + d[prefsum-k]

			if prefsum not in d:
				d[prefsum] = 1
			else:
				d[prefsum] = d[prefsum]+1

		return ans
/*
*/
class Solution {
    public int subarraySum(int[] nums, int k) {

        HashMap<Integer, Integer> map = new HashMap<>();
        map.put(0,1);
        int count = 0;
        int sum = 0;

        for(int i=0; i<nums.length; i++){
            sum += nums[i];
            if(map.containsKey(sum - k)){
                count += map.get(sum-k);
            }
            map.put(sum, map.getOrDefault(sum,0)+1);
        }
        return count;
    }
}
/********************************* Solution Using HashMap / Prefix ********************/

class Solution {
public:
    int subarraySum(vector<int>& nums, int k) {
       
        unordered_map<int, int> prefixSum {{0, 1}};
        
        int sum = 0;
        int numOfSubArr = 0;
        int size = nums.size();
        
        
        for (int index = 0; index < size; index++){
            
            sum += nums[index];
            if (prefixSum.find(sum-k) != prefixSum.end()) 
                numOfSubArr += prefixSum[sum-k];
            
            prefixSum[sum]++;
        }
        
        return numOfSubArr;
    }
};
    var subarraySum = function(nums, k) {
        const obj = {};
        let res = 0;
        let sum = 0;

        for (let i = 0; i < nums.length; i++) {

            sum += nums[i];
            if (sum == k) res++;

            if (obj[sum - k]) res += obj[sum - k];

            obj[sum] ? obj[sum] += 1 : obj[sum] = 1;
        }
        return res;
    };
Subarray Sum Equals K