##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
You know knapsack patterns. Now you need a tool that speeds up range computations. Prefix sums turn O(n) range queries into O(1).
The Goal
Why we need this
Loop every query
The definition
One pass
The formula
The code
Tracing the algorithm
Getting indices right
Knowledge check
Query range sums fast
Applying the technique
Two prefix arrays
Prefix[r] - prefix[l-1]
Sorted and original
Applying prefix sums
Two array variant
Understanding the tradeoff
Complete solution
The code
HashMap prefix counting
Count with frequency
The code
HashMap technique trace
Special case of k=0
Knowledge check
Prefix and suffix products
Two directions
Prefix and suffix products
Handling zeros
One output array
2D prefix sum queries
Inclusion-exclusion
2D prefix sums trace
Counting with target sum
The code
Knowledge check
Kadane connection
Kadane's algorithm
Comparing approaches
summary
Recognizing the pattern
Preview of LIS
What we learned