01
Overview

Averaging an element with its neighbors.

For each position, compute the mean of itself plus its left and right neighbors — carefully at the array's ends.

Neighbor averaging smooths a sequence by replacing each value with the average of itself and its immediate neighbors. It's the 1D version of image blurring or signal smoothing.

The boundary cells (index 0 and the last) have only one neighbor instead of two. The cleanest fix: count the actual in-bounds neighbors and divide by that count.

On the AP exam, this appears in FRQs about moving averages, signal smoothing, or time-series cleanup.

02
Core Concept

Build a new array; check both neighbors per cell.

Read from the original; write to the result.

Three-cell average with boundary handling

public double[] smooth(int[] arr) {
    double[] result = new double[arr.length];
    for (int i = 0; i < arr.length; i++) {
        int sum = arr[i];
        int count = 1;
        if (i > 0) { sum += arr[i - 1]; count++; }
        if (i < arr.length - 1) { sum += arr[i + 1]; count++; }
        result[i] = (double) sum / count;
    }
    return result;
}

Why a result array?

If you mutated the original in place, later cells would read already-smoothed values, contaminating the result. A separate result array preserves the input throughout.

Boundary counts

  • Index 0: count = 2 (itself + right neighbor).
  • Last index: count = 2 (itself + left neighbor).
  • Interior: count = 3.
03
Key Vocabulary

Smoothing vocabulary.

Used in signal-processing problems.

Vocabulary
  • Neighbor average — mean of a cell plus adjacent cells.
  • Boundary cell — index 0 or the last index.
  • Interior cell — neither boundary.
  • Smoothing — replacing values with local averages.
  • Result array — the new array storing computed averages.
  • Actual neighbor count — the divisor, varies by position.
04
AP Exam Focus

How neighbor averaging is tested.

Boundary divisor and in-place hazard dominate.

Exam Focus
  • Divide by actual count. Not always 3.
  • Use a result array. Avoids read-after-write hazards.
  • Cast for accurate average. Otherwise integer division.
  • Bounds checks. Don't access arr[-1] or arr[length].
05
Worked Example

Smooth a small array.

Verify divisor at boundaries and interior.

Worked Example AP-style reasoning

Input: [2, 4, 6, 8].

Trace:

  • i=0: sum = 2+4 = 6; count = 2; avg = 3.0.
  • i=1: sum = 2+4+6 = 12; count = 3; avg = 4.0.
  • i=2: sum = 4+6+8 = 18; count = 3; avg = 6.0.
  • i=3: sum = 6+8 = 14; count = 2; avg = 7.0.

Result: [3.0, 4.0, 6.0, 7.0].

06
Common Mistakes

Neighbor-averaging pitfalls.

Most come from wrong divisor or in-place mutation.

Watch Out
  • Dividing by 3 always. Wrong at boundaries.
  • Mutating in place. Corrupts later averages.
  • No bounds check. Accessing arr[-1].
  • Integer division. Cast or use double.
07
Reference Table

Neighbor count by position.

The divisor varies.

Neighbor Avg Reference

AP Quick Reference
PositionNeighbors countedDivisor
Index 0Self + right2
Last indexSelf + left2
InteriorSelf + left + right3
1-element arraySelf only1
08
Practice Tip

How to master neighbor averaging.

Track count along with sum.

Practice Strategy
  • MCQ Practice — verify divisor at index 0 and last index.
  • Java Lab — implement smooth with a result array; test on 1-, 2-, and many-element inputs.
  • FRQ Practice — never mutate in place when neighbors are read; always cast.
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 11 Neighbor Averaging with Boundaries FRQ Practice

AP-style topic practice assessment

Start