Running Sum of 1D Array — Prefix Sum Foundation [Amazon Easy]
Compute running (prefix) sum in O(n) time O(1) extra space. The prefix sum pattern is foundational for range queries, subarray problems and 2D matrices.
webcoderspeed.com
24 articles
Compute running (prefix) sum in O(n) time O(1) extra space. The prefix sum pattern is foundational for range queries, subarray problems and 2D matrices.
Compute product of all elements except self without division. O(n) two-pass prefix/suffix approach with O(1) extra space.
Count subarrays summing to k. Classic prefix sum + HashMap trick: if prefix[j]-prefix[i]=k then prefix[i]=prefix[j]-k. O(n) time.
Find the shortest subarray with sum >= K using a monotonic deque on prefix sums, handling negative numbers correctly.
Count the number of range sums that lie in [lower, upper] using a merge sort approach on prefix sums.
Count subarrays with sum in [lower, upper] using merge sort on prefix sums for O(n log n) complexity.
Count subarrays with sum exactly k using prefix sums stored in a HashMap to find valid starting positions in O(n).
Check if a continuous subarray of length >= 2 sums to a multiple of k using prefix sum modulo k with a HashMap.
Implement weighted random selection by building a prefix sum array and using binary search to find the sampled index.
Find the smallest subarray to remove so the remaining sum is divisible by p using prefix modular arithmetic.
Count subarrays with sum divisible by k using prefix mod frequencies and the complement map pattern.
Remove consecutive nodes that sum to zero by tracking prefix sums in a map and relinking past zero-sum runs.
Sum of rectangle region. Precompute 2D prefix sums: prefix[i][j] = sum of all cells from (0,0) to (i-1,j-1). Inclusion-exclusion for query.
Find max rectangle sum <= k in 2D matrix. Fix row boundaries, compress to 1D array, use prefix sums + BIT/sorted set to find best subarray sum <= k.
Find longest subarray with sum <= k. Build prefix sums, use monotonic deque for decreasing prefix sums to efficiently find left boundaries.
Find the shortest subarray with sum >= k using a monotonic deque on prefix sums for O(n) time.
Count paths in a binary tree summing to target (not necessarily root-to-leaf) using a prefix sum frequency map.
Compute the running sum of a 1D array where each element is the sum of itself and all previous elements.
Count subarrays with exactly k odd numbers using the at-most(k) minus at-most(k-1) sliding window formula.
Count binary subarrays with exactly the given sum using prefix sum with HashMap or the atMost(k)-atMost(k-1) trick.
Count subarrays with sum divisible by k using prefix sum modulo k and a frequency map of remainders.
Count subarrays where the median is exactly k by converting the problem to counting balanced prefix sequences around k.
Find the shortest subarray with sum >= k, handling negative numbers correctly using a monotonic deque on prefix sums.
Count subarrays with sum equal to k using prefix sums and a hashmap. O(n) time by tracking how many times each prefix sum has occurred.