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.
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.
Smoothing vocabulary.
Used in signal-processing problems.
- 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.
How neighbor averaging is tested.
Boundary divisor and in-place hazard dominate.
- 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]orarr[length].
Smooth a small array.
Verify divisor at boundaries and interior.
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].
Neighbor-averaging pitfalls.
Most come from wrong divisor or in-place mutation.
- 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.
Neighbor count by position.
The divisor varies.
Neighbor Avg Reference
AP Quick Reference| Position | Neighbors counted | Divisor |
|---|---|---|
| Index 0 | Self + right | 2 |
| Last index | Self + left | 2 |
| Interior | Self + left + right | 3 |
| 1-element array | Self only | 1 |
How to master neighbor averaging.
Track count along with sum.
- 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.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 07 Topic 11 Neighbor Averaging with Boundaries FRQ Practice
AP-style topic practice assessment