To find out how many subarrays have a sum equal to k, note that if we have a sum equal to say 7, and a k of 2, and we have an array that sums up to 5, we know that we have as many arrays that sum to k as there are arrays that sum to 5. We put that in the code and return that.

class Solution:
    def subarraySum(self, nums: List[int], k: int) -> int:
        cache = defaultdict(int) 
        cache[0] = 1
        count = total = 0
        for n in nums:
            total += n
            count += cache[total - k]
            cache[total] += 1
        
        return count