Distances between adjacent elements.
Compare each value with its right-hand neighbor — the foundation of jump detection, smoothness analysis, and difference arrays.
An adjacent pair is two consecutive elements at indices i and i + 1. Their distance — usually the absolute difference — measures how much the array changed in one step.
The loop bound shrinks: only indices 0 through length - 2 have a right neighbor. Forgetting this is the classic off-by-one trap.
On the AP exam, this pattern appears in problems involving sequence smoothness, biggest jumps, or detecting trend reversals.
Loop to length - 1; access i + 1 safely.
Stop one short to keep the right neighbor in bounds.
Building a distance array
public int[] adjacentDistances(int[] arr) {
int[] gaps = new int[arr.length - 1];
for (int i = 0; i < arr.length - 1; i++) {
gaps[i] = Math.abs(arr[i + 1] - arr[i]);
}
return gaps;
}
The result array has length arr.length - 1 — one entry per pair.
Finding the largest jump
int maxGap = 0;
for (int i = 0; i < arr.length - 1; i++) {
int gap = Math.abs(arr[i + 1] - arr[i]);
if (gap > maxGap) maxGap = gap;
}
Why Math.abs?
Without it, descending pairs produce negative differences. Absolute value gives a true distance independent of direction.
Pair-distance vocabulary.
Used in difference-based problems.
- Adjacent pair — consecutive elements at
iandi + 1. - Distance / gap — absolute difference between the two.
- Difference array — array of all adjacent differences.
- Pair count — there are
length - 1pairs in an array oflength. - Off-by-one — looping too far and accessing
arr[length].
How pair distances are tested.
Loop bound and absolute value dominate.
- Loop to
length - 1. Going tolengththrows an exception. - Use
Math.abs. Direction usually doesn't matter for distance. - Result-array length. One less than the source array.
Compute all gaps.
Verify length and absolute values.
Input: [10, 4, 7, 7, 20].
Trace:
- i=0: |4 - 10| = 6.
- i=1: |7 - 4| = 3.
- i=2: |7 - 7| = 0.
- i=3: |20 - 7| = 13.
Result: [6, 3, 0, 13]. Length 4 — one less than input length 5.
Largest jump: 13 (between index 3 and 4).
Pair-distance pitfalls.
Most are off-by-one or sign errors.
- Wrong loop bound.
i < arr.lengthcrashes; usearr.length - 1. - Forgetting
Math.abs. Produces negative "distances". - Result array sized wrong. Must be
length - 1, notlength. - Accessing
arr[i - 1]without a starting guard. The mirrored pattern needsi = 1to start.
Adjacent-pair patterns.
Loop bound and result size in one place.
Pair Distance Reference
AP Quick Reference| Item | Value | Why |
|---|---|---|
| Loop bound | i < arr.length - 1 | Right neighbor must exist. |
| Right neighbor | arr[i + 1] | Safe within bound. |
| Distance | Math.abs(arr[i+1] - arr[i]) | Direction-independent. |
| Number of pairs | arr.length - 1 | One per gap. |
How to master pair distances.
Count pairs before writing the loop.
- MCQ Practice — for each option, confirm loop bound is
length - 1and absolute value is used. - Java Lab — write adjacentDistances, maxGap, smallestGap. Verify result length.
- FRQ Practice — for "consecutive difference" prompts, write the shrunk loop bound from memory.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 07 Topic 02 Adjacent-Pair Distances FRQ Practice
AP-style topic practice assessment