Data Structures19 sections · 729 units
Open in Course

Problem - Subarray Sum Equals K

Prefix + hash combo

Given an integer array numsnums and an integer kk, return the total number of subarrays whose sum equals kk. You're solving the textbook prefix sum + hash map problem.

The combination gives O(n)O(n) time. Note: the array can contain negative numbers, so the simple sliding window approach doesn't work.