Dynamic Programming21 sections · 916 units
Open in Course

LeetCode 560 Subarray Sum Equals K - Problem Statement

HashMap prefix counting

This is Subarray Sum Equals K from LeetCode. It's one of the most common prefix sum interview questions. Given an array of integers numsnums and an integer kk, return the total number of subarrays whose sum equals kk.

At first, you might try checking every subarray. That's O(n2)O(n^2) subarrays, and even with prefix sums, you're still doing O(n2)O(n^2) comparisons. Can you do better?