Prefix Sums

You know knapsack patterns. Now you need a tool that speeds up range computations. Prefix sums turn O(n) range queries into O(1).

44 lessons
162 min
Codeforces: 900-1800LeetCode: 1200-1700

Lessons

1. Intro

The Goal

4m

2. The Range Sum Problem

Why we need this

4m

3. Naive Approach

Loop every query

4m

4. Vocabulary - Prefix Sum

The definition

3m

5. Building the Prefix Array

One pass

4m

6. Answering Range Queries

The formula

3m

7. Prefix Sums - Implementation

The code

3m

8. Prefix Sums - Walkthrough

Tracing the algorithm

4m

9. Challenge: Off-by-One Errors

Getting indices right

4m

10. Quiz: Prefix Sums Basics

Knowledge check

3m1 problems

11. Static Range Sum Queries - Problem Statement

Query range sums fast

3m1 problems

12. Static Range Sum Queries - Why Prefix Sums

Applying the technique

4m

13. Codeforces 869B Kuriyama Mirai's Stones - Problem Statement

Two prefix arrays

3m1 problems

14. Static Range Sum Queries - The Formula

Prefix[r] - prefix[l-1]

4m

15. Codeforces 869B Kuriyama Mirai's Stones - Two Arrays

Sorted and original

3m

16. Static Range Sum Queries - Walkthrough

Applying prefix sums

4m

17. Codeforces 869B Kuriyama Mirai's Stones - Walkthrough

Two array variant

4m

18. Quiz: Range Query Complexity

Understanding the tradeoff

3m1 problems

19. Static Range Sum Queries - Implementation

Complete solution

5m1 problems

20. Codeforces 869B Kuriyama Mirai's Stones - Implementation

The code

4m1 problems

21. LeetCode 560 Subarray Sum Equals K - Problem Statement

HashMap prefix counting

3m1 problems

22. LeetCode 560 Subarray Sum Equals K - HashMap Trick

Count with frequency

4m

23. LeetCode 560 Subarray Sum Equals K - Implementation

The code

4m1 problems

24. LeetCode 560 Subarray Sum Equals K - Walkthrough

HashMap technique trace

4m

25. Challenge: Subarray Sum Zero

Special case of k=0

4m

26. Quiz: Prefix Sum + HashMap

Knowledge check

3m1 problems

27. LeetCode 238 Product of Array Except Self - Problem Statement

Prefix and suffix products

3m1 problems

28. LeetCode 238 Product of Array Except Self - Prefix and Suffix

Two directions

4m

29. LeetCode 238 Product of Array Except Self - Walkthrough

Prefix and suffix products

4m

30. Quiz: Product Edge Cases

Handling zeros

3m1 problems

31. LeetCode 238 Product of Array Except Self - Space Optimization

One output array

4m1 problems

32. LeetCode 304 Range Sum Query 2D - Problem Statement

2D prefix sum queries

3m1 problems

33. LeetCode 304 Range Sum Query 2D - The Concept

Inclusion-exclusion

4m

34. LeetCode 304 Range Sum Query 2D - Walkthrough

2D prefix sums trace

4m

35. Challenge: 2D Submatrix Count

Counting with target sum

4m

36. LeetCode 304 Range Sum Query 2D - Implementation

The code

4m1 problems

37. Quiz: 2D Prefix Sums

Knowledge check

3m1 problems

38. LeetCode 53 Maximum Subarray Sum - Problem Statement

Kadane connection

3m

39. LeetCode 53 Maximum Subarray Sum - Implementation

Kadane's algorithm

5m

40. Quiz: Kadane vs Prefix

Comparing approaches

3m1 problems

41. Lessons from Prefix Sums

summary

3m

42. Pattern - When to Use Prefix Sums

Recognizing the pattern

4m

43. What's Next

Preview of LIS

4m

44. Section Recap

What we learned

5m

Practice Problems

1.
Special ElementsCodeforceseasy

Prefix sum DP - teaches how to precompute and reuse subproblem results

2.

Classic 2D prefix sum problem with constant-time rectangle queries.

3.
Subarray Sum Equals KLeetCodemedium

Standard prefix sum + hashmap pattern used everywhere.

4.

Handles negative numbers with prefix sum + hashmap technique.

5.

Teaches prefix sum optimization to reduce O(NK²) to O(NK).

6.

Demonstrates when greedy+recursion outperforms pure recursion

7.

Combines 2D prefix sums with hashmap for matrix subarray counting.

8.

Covered with full walkthrough in this section.

9.

Covered with full walkthrough in this section.

10.

Covered with full walkthrough in this section.

11.

Covered with full walkthrough in this section.

12.
Prefix Sum PrimesCodeforcesmedium

Arrange tiles to maximize prime prefix sums. Teaches creative use of prefix sums with number theory.

13.
Prefix Sum AddictsCodeforcesmedium

Reverse engineer a sorted sequence from partial prefix sums. Tests deep understanding of prefix sum properties.

14.
Least Prefix SumCodeforcesmedium

Minimize operations to make a prefix sum the global minimum. Combines greedy with prefix sum analysis.

15.

Count maximal prefix sums across all permutations. Beautiful combinatorics with prefix sums.

16.

Find subarray with sum divisible by k. Classic prefix sum + modular arithmetic + hashmap pattern.

17.

Count subarrays divisible by k. Prefix sum with modular counting - interview favorite.

18.
Contiguous ArrayLeetCodemedium

Longest subarray with equal 0s and 1s. Transform to prefix sum problem with hashmap.

19.
Good SubarraysCodeforcesmedium

Count subarrays where sum equals length. Prefix sum + hashmap counting pattern.

20.
Count TrianglesCodeforcesmedium

Count valid triangles with prefix sums. Transforms geometry to array problem.

21.
Unstable StringCodeforcesmedium

Count alternating substrings with wildcards. Prefix counting with state tracking.

22.
Rock Paper ScissorsCodeforcesmedium

Maximize wins in RPS with prefix sums. Sliding window + prefix optimization.

23.

Connect forests without creating cycles. DSU with prefix optimization.

24.
Maximum Crossings (Hard)Codeforcesmedium

Count inversions with merge sort. Classic divide and conquer application.

25.
Maximum SubarrayLeetCodemedium

Simple usage of prefix sum

26.
FlowersCodeforcesmedium

2D DP with prefix sums combining multiple DP techniques

Ready to start learning?

Access all 44 lessons with interactive content and progress tracking.