Given an integer array nums and an integer k, count the number of contiguous subarrays whose sum equals k. Solve it in O(n) time and O(n) space by maintaining a running prefix sum and a hash map of prefix-sum frequencies. Explain why this works with negative numbers, analyze complexity, and provide code or pseudocode.