01
Overview

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.

02
Core Concept

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.

03
Key Vocabulary

Segment vocabulary.

Used in range and window problems.

Vocabulary
  • Segment / slice — contiguous range from start to end.
  • Window — segment of a fixed size k.
  • Inclusive boundary — endpoint is part of the range.
  • Window countarr.length - k + 1.
  • Outer / inner loop — choose start; sum the range.
04
AP Exam Focus

How segment sums are tested.

Inclusive boundaries and window bounds are common traps.

Exam Focus
  • 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.
05
Worked Example

Best 3-day stretch.

Window scanning at work.

Worked Example AP-style reasoning

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).

06
Common Mistakes

Segment-sum pitfalls.

All come from boundary errors.

Watch Out
  • < vs <= on inclusive endpoints.
  • Outer bound too generous. start < arr.length crashes when window extends past the end.
  • Not resetting sum per window. Sums accumulate across windows.
  • Off-by-one in inner bound. i <= start + k grabs one extra element.
07
Reference Table

Segment and window bounds.

The boundary math at a glance.

Segment Sum Reference

AP Quick Reference
OperationLoop boundAP Tip
Sum [start, end]i <= endInclusive.
Window outerstart <= length - kEnsures window fits.
Window inneri < start + kExactly k elements.
Window countlength - k + 1Common MCQ answer.
08
Practice Tip

How to master segment sums.

Verify boundaries on tiny examples.

Practice Strategy
  • 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.
09
Section · 09

Practice — attempt these now.

AP-style assessments aligned to this lesson. Time them.

FRQ Practice 30 min 18 marks Pending

AP CSA Unit 07 Topic 07 Contiguous Segment Sums FRQ Practice

AP-style topic practice assessment

Start