Prefix Sum pattern
Subarray Sum Equals K
Given an integer array and an integer k, return the total number of contiguous subarrays whose elements sum to exactly k. Watch it run step by step, then write your own pseudocode and get it checked by AI.
- LeetCode 560
- Medium
- 2 approaches
Sign in with Google or GitHub and you land straight on this page. DSA comes with the all labs plan and every ChaiCode batch.
Prefix SumLeetCode 560
Medium
From the first idea to the best one
- 01 Brute force · every subarray O(n²)O(1) space
- 02 Optimized · prefix + hashmap O(n)O(n) space
What you will learn
- How to read Subarray Sum Equals K and spot the prefix sum pattern in it
- Brute force · every subarray: O(n²) time, O(1) space
- Optimized · prefix + hashmap: O(n) time, O(n) space
- The pseudocode for each approach, its line lit as the animation runs
- Practice: write your own pseudocode and get it checked
More Prefix Sum
See Subarray Sum Equals K run, step by step
Sign in and this page opens on the animation, with all 2 Prefix Sum problems beside it.
DSA comes with the all labs plan and every ChaiCode batch.