Summing contiguous segments of an array.
Sum a slice between two indices, or every fixed-width window — both rest on the same loop pattern.
A contiguous segment is a slice arr[start..end] — every element between two indices. Computing segment sums is a building block for window analysis, range queries, and rolling statistics.
The variations: sum one fixed segment; sum every window of size k; find the max window sum. All share the same nested or windowed pattern.
On the AP exam, segment-sum problems appear in FRQs about moving averages, score windows, and "best k-day stretch" problems.
Inclusive boundaries; loop the inner range.
For windows of size k, the outer bound shrinks by k - 1.
Sum a fixed segment
public int segmentSum(int[] arr, int start, int end) {
int sum = 0;
for (int i = start; i <= end; i++) {
sum += arr[i];
}
return sum;
}
The <= makes both endpoints inclusive.
Window-of-k sums
public int maxWindowSum(int[] arr, int k) {
int best = Integer.MIN_VALUE;
for (int start = 0; start <= arr.length - k; start++) {
int sum = 0;
for (int i = start; i < start + k; i++) {
sum += arr[i];
}
if (sum > best) best = sum;
}
return best;
}
Outer loop has arr.length - k + 1 windows; inner loop sums each.
Segment vocabulary.
Used in range and window problems.
- Segment / slice — contiguous range from
starttoend. - Window — segment of a fixed size
k. - Inclusive boundary — endpoint is part of the range.
- Window count —
arr.length - k + 1. - Outer / inner loop — choose start; sum the range.
How segment sums are tested.
Inclusive boundaries and window bounds are common traps.
- Use
<=for inclusive end.<excludes the last cell. - Window outer bound:
start <= arr.length - k. - Inner window bound:
i < start + k. - Window count:
arr.length - k + 1.
Best 3-day stretch.
Window scanning at work.
Input: [2, 1, 5, 1, 3, 2], k = 3.
Windows:
- start=0: 2+1+5 = 8.
- start=1: 1+5+1 = 7.
- start=2: 5+1+3 = 9.
- start=3: 1+3+2 = 6.
Window count: 6 - 3 + 1 = 4. Max: 9 (from indices 2-4).
Segment-sum pitfalls.
All come from boundary errors.
<vs<=on inclusive endpoints.- Outer bound too generous.
start < arr.lengthcrashes when window extends past the end. - Not resetting
sumper window. Sums accumulate across windows. - Off-by-one in inner bound.
i <= start + kgrabs one extra element.
Segment and window bounds.
The boundary math at a glance.
Segment Sum Reference
AP Quick Reference| Operation | Loop bound | AP Tip |
|---|---|---|
| Sum [start, end] | i <= end | Inclusive. |
| Window outer | start <= length - k | Ensures window fits. |
| Window inner | i < start + k | Exactly k elements. |
| Window count | length - k + 1 | Common MCQ answer. |
How to master segment sums.
Verify boundaries on tiny examples.
- MCQ Practice — count the windows manually before picking an answer.
- Java Lab — implement segmentSum, maxWindowSum, and minWindowSum. Verify counts with arr.length - k + 1.
- FRQ Practice — write both loop bounds carefully. Sketch the windows on paper first.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 07 Topic 07 Contiguous Segment Sums FRQ Practice
AP-style topic practice assessment