Arrays & Prefix Sums
Arrays are everywhere. Learn prefix sums for O(1) range queries and difference arrays for updates.
Lessons
1. Intro
Why arrays matter
2. Array Fundamentals
What you already know
3. The Range Sum Problem
A common bottleneck
4. Vocabulary - Prefix Sum
Cumulative totals
5. The Core Idea
Subtraction trick
6. Visualization
See it work
7. Problem - Range Sum Query
Classic prefix sum
8. Range Sum - Building the Array
One pass construction
9. Range Sum - Answering Queries
Constant time lookup
10. Range Sum - Implementation
Complete solution
11. Lessons from Range Sum
summary
12. Quiz: Prefix Sum Basics
Range query formula
13. Extending to 2D
Matrix range sums
14. Vocabulary - 2D Prefix Sum
Rectangle sums
15. 2D Prefix - The Formula
Inclusion-exclusion
16. 2D Prefix - Building
Construction formula
17. Problem - 2D Range Sum
Matrix queries
18. 2D Range Sum - Build
Preprocessing step
19. 2D Range Sum - Query
Four-term formula
20. Lessons from 2D Prefix
Pattern recognition
21. Quiz: 2D Prefix Sums
Inclusion-exclusion
22. The Range Update Problem
A different challenge
23. Vocabulary - Difference Array
Inverse of prefix sum
24. Difference Array Insight
How updates work
25. Difference Array Visualization
Trace an example
26. Problem - Range Addition
Apply difference arrays
27. Range Addition - Algorithm
Two-phase approach
28. Range Addition - Implementation
Complete solution
29. Lessons from Difference Arrays
Duality principle
30. Quiz: Difference Arrays
Update mechanics
31. The Pair Finding Problem
A quadratic trap
32. Vocabulary - Two Pointers
Moving indices
33. Two Pointers Logic
The decision rule
34. Problem - Two Sum Sorted
Classic two pointers
35. Two Sum Sorted - Why It Works
Proof sketch
36. Two Sum Sorted - Implementation
Clean solution
37. Lessons from Two Pointers
When to use it
38. Quiz: Two Pointers
Pointer movement
39. The Subarray Problem
Contiguous elements
40. Vocabulary - Sliding Window
Moving subarray
41. Fixed Window Pattern
Size k subarrays
42. Problem - Maximum Average Subarray
Fixed window intro
43. Max Average - Implementation
Sliding sum
44. Variable Window Pattern
Dynamic sizing
45. Problem - Longest Substring
Variable window classic
46. Longest Substring - The Idea
When to shrink
47. Longest Substring - Implementation
Complete solution
48. Lessons from Sliding Window
Recognizing the pattern
49. Quiz: Sliding Window
Time complexity
50. Section Recap
What we learned
Practice Problems
Combines sorting, prefix sum, and binary search in one problem. Learn how to precompute cumulative sums and answer range queries efficiently.
Perfect introduction to sliding window with prefix sums. Find k consecutive planks with minimum height using the add-one-remove-one pattern.
Teaches maintaining multiple prefix sum arrays (original and sorted). Shows when to precompute different views of data for O(1) queries.
Greedy approach with implicit prefix tracking. Find maximum sum of alternating positive/negative subsequence by identifying consecutive segments.
Classic two-pointer/sliding window problem. Find maximum number of consecutive books within time limit - fundamental pattern for subarray problems.
Advanced prefix sum with hashmap technique. Count subarrays where sum equals length by transforming to 'sum equals 0' problem.
Masterclass in prefix/suffix counting. Split array into 3 parts with equal sums - requires tracking valid split points efficiently.
THE problem to learn difference arrays. Handle range updates with nested operations - essential for understanding lazy propagation.
Transform comparison into prefix sum problem. Count pairs where a[i]+a[j] > b[i]+b[j] using difference array and sorting.
Prefix sum with divisibility insight. Minimum operations to make all equal by merging neighbors - check equal-sum segment partitions.
Implicit prefix tracking with state management. Partition binary string into minimum alternating subsequences using queue-based approach.
Greedy with prefix sum optimization using priority queue. Minimize operations to make prefix sum at position m the smallest.
Perfect introduction to prefix sum concept with straightforward implementation. Builds foundational understanding of cumulative sums.
The quintessential prefix sum + hashmap problem that teaches how to count subarrays with a target sum in O(n) time.
The iconic Kadane's algorithm for finding maximum subarray sum in O(n) time. Fundamental greedy algorithm every programmer must know.
Brilliant application of prefix and suffix products without division. Two-pass technique for O(n) solutions with O(1) extra space.
Non-obvious two-pointer greedy approach where moving the shorter pointer maximizes potential area.
Combines sorting with two-pointer technique to reduce O(n^3) to O(n^2). Must-know for interview preparation.
The fundamental 2D prefix sum problem with inclusion-exclusion principle. Essential for 2D range query problems.
Combines 2D prefix sums with the 'Subarray Sum Equals K' technique. The ultimate 2D prefix sum challenge.