15% off all plans & roadmaps Use code
See plansProblem editorial
Given an integer array nums and an integer k, consider every non-empty subarray of nums. A subarray is a contiguous sequence of one or more elements, and its sum is the total of its elements.
Subarray Sum Equals K is a Array/Matrix Manipulation Patterns problem, in the Hashing - Frequency Map / Counting subpattern. The pattern covers in-place operations, hashing, prefix sums, spiral traversal, and cyclic sort
The reference solution runs in O(n) average time time and O(n) space.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Subarray Sum Equals K is a Array/Matrix Manipulation Patterns problem, in the Hashing - Frequency Map / Counting subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.