Arrays & Prefix Sums

Arrays are everywhere. Learn prefix sums for O(1) range queries and difference arrays for updates.

50 lessons
135 min

Lessons

1. Intro

Why arrays matter

2m

2. Array Fundamentals

What you already know

2m

3. The Range Sum Problem

A common bottleneck

2m

4. Vocabulary - Prefix Sum

Cumulative totals

3m

5. The Core Idea

Subtraction trick

3m

6. Visualization

See it work

3m

7. Problem - Range Sum Query

Classic prefix sum

3m1 problems

8. Range Sum - Building the Array

One pass construction

3m

9. Range Sum - Answering Queries

Constant time lookup

2m

10. Range Sum - Implementation

Complete solution

3m1 problems

11. Lessons from Range Sum

summary

2m

12. Quiz: Prefix Sum Basics

Range query formula

2m1 problems

13. Extending to 2D

Matrix range sums

2m

14. Vocabulary - 2D Prefix Sum

Rectangle sums

3m

15. 2D Prefix - The Formula

Inclusion-exclusion

4m

16. 2D Prefix - Building

Construction formula

3m

17. Problem - 2D Range Sum

Matrix queries

3m1 problems

18. 2D Range Sum - Build

Preprocessing step

4m

19. 2D Range Sum - Query

Four-term formula

3m1 problems

20. Lessons from 2D Prefix

Pattern recognition

2m

21. Quiz: 2D Prefix Sums

Inclusion-exclusion

2m1 problems

22. The Range Update Problem

A different challenge

2m

23. Vocabulary - Difference Array

Inverse of prefix sum

3m

24. Difference Array Insight

How updates work

3m

25. Difference Array Visualization

Trace an example

3m

26. Problem - Range Addition

Apply difference arrays

3m1 problems

27. Range Addition - Algorithm

Two-phase approach

2m

28. Range Addition - Implementation

Complete solution

4m1 problems

29. Lessons from Difference Arrays

Duality principle

2m

30. Quiz: Difference Arrays

Update mechanics

2m1 problems

31. The Pair Finding Problem

A quadratic trap

2m

32. Vocabulary - Two Pointers

Moving indices

3m

33. Two Pointers Logic

The decision rule

3m

34. Problem - Two Sum Sorted

Classic two pointers

3m1 problems

35. Two Sum Sorted - Why It Works

Proof sketch

3m

36. Two Sum Sorted - Implementation

Clean solution

3m1 problems

37. Lessons from Two Pointers

When to use it

2m

38. Quiz: Two Pointers

Pointer movement

2m1 problems

39. The Subarray Problem

Contiguous elements

2m

40. Vocabulary - Sliding Window

Moving subarray

3m

41. Fixed Window Pattern

Size k subarrays

3m

42. Problem - Maximum Average Subarray

Fixed window intro

3m1 problems

43. Max Average - Implementation

Sliding sum

3m1 problems

44. Variable Window Pattern

Dynamic sizing

3m

45. Problem - Longest Substring

Variable window classic

3m1 problems

46. Longest Substring - The Idea

When to shrink

3m

47. Longest Substring - Implementation

Complete solution

4m1 problems

48. Lessons from Sliding Window

Recognizing the pattern

2m

49. Quiz: Sliding Window

Time complexity

2m1 problems

50. Section Recap

What we learned

3m

Practice Problems

1.
Eating QueriesCodeforceseasy

Combines sorting, prefix sum, and binary search in one problem. Learn how to precompute cumulative sums and answer range queries efficiently.

2.
FenceCodeforceseasy

Perfect introduction to sliding window with prefix sums. Find k consecutive planks with minimum height using the add-one-remove-one pattern.

3.

Teaches maintaining multiple prefix sum arrays (original and sorted). Shows when to precompute different views of data for O(1) queries.

4.

Greedy approach with implicit prefix tracking. Find maximum sum of alternating positive/negative subsequence by identifying consecutive segments.

5.
BooksCodeforcesmedium

Classic two-pointer/sliding window problem. Find maximum number of consecutive books within time limit - fundamental pattern for subarray problems.

6.
Good SubarraysCodeforcesmedium

Advanced prefix sum with hashmap technique. Count subarrays where sum equals length by transforming to 'sum equals 0' problem.

7.
Number of WaysCodeforcesmedium

Masterclass in prefix/suffix counting. Split array into 3 parts with equal sums - requires tracking valid split points efficiently.

8.
Greg and ArrayCodeforcesmedium

THE problem to learn difference arrays. Handle range updates with nested operations - essential for understanding lazy propagation.

9.
Pair of TopicsCodeforcesmedium

Transform comparison into prefix sum problem. Count pairs where a[i]+a[j] > b[i]+b[j] using difference array and sorting.

10.

Prefix sum with divisibility insight. Minimum operations to make all equal by merging neighbors - check equal-sum segment partitions.

11.

Implicit prefix tracking with state management. Partition binary string into minimum alternating subsequences using queue-based approach.

12.
Least Prefix SumCodeforceshard

Greedy with prefix sum optimization using priority queue. Minimize operations to make prefix sum at position m the smallest.

13.

Perfect introduction to prefix sum concept with straightforward implementation. Builds foundational understanding of cumulative sums.

14.
Subarray Sum Equals KLeetCodemedium

The quintessential prefix sum + hashmap problem that teaches how to count subarrays with a target sum in O(n) time.

15.
Maximum SubarrayLeetCodemedium

The iconic Kadane's algorithm for finding maximum subarray sum in O(n) time. Fundamental greedy algorithm every programmer must know.

16.

Brilliant application of prefix and suffix products without division. Two-pass technique for O(n) solutions with O(1) extra space.

17.

Non-obvious two-pointer greedy approach where moving the shorter pointer maximizes potential area.

18.
3SumLeetCodemedium

Combines sorting with two-pointer technique to reduce O(n^3) to O(n^2). Must-know for interview preparation.

19.

The fundamental 2D prefix sum problem with inclusion-exclusion principle. Essential for 2D range query problems.

20.

Combines 2D prefix sums with the 'Subarray Sum Equals K' technique. The ultimate 2D prefix sum challenge.

Ready to start learning?

Access all 50 lessons with interactive content and progress tracking.