01
Overview

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.

02
Core Concept

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.

03
Key Vocabulary

Pair-distance vocabulary.

Used in difference-based problems.

Vocabulary
  • Adjacent pair — consecutive elements at i and i + 1.
  • Distance / gap — absolute difference between the two.
  • Difference array — array of all adjacent differences.
  • Pair count — there are length - 1 pairs in an array of length.
  • Off-by-one — looping too far and accessing arr[length].
04
AP Exam Focus

How pair distances are tested.

Loop bound and absolute value dominate.

Exam Focus
  • Loop to length - 1. Going to length throws an exception.
  • Use Math.abs. Direction usually doesn't matter for distance.
  • Result-array length. One less than the source array.
05
Worked Example

Compute all gaps.

Verify length and absolute values.

Worked Example AP-style reasoning

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

06
Common Mistakes

Pair-distance pitfalls.

Most are off-by-one or sign errors.

Watch Out
  • Wrong loop bound. i < arr.length crashes; use arr.length - 1.
  • Forgetting Math.abs. Produces negative "distances".
  • Result array sized wrong. Must be length - 1, not length.
  • Accessing arr[i - 1] without a starting guard. The mirrored pattern needs i = 1 to start.
07
Reference Table

Adjacent-pair patterns.

Loop bound and result size in one place.

Pair Distance Reference

AP Quick Reference
ItemValueWhy
Loop boundi < arr.length - 1Right neighbor must exist.
Right neighborarr[i + 1]Safe within bound.
DistanceMath.abs(arr[i+1] - arr[i])Direction-independent.
Number of pairsarr.length - 1One per gap.
08
Practice Tip

How to master pair distances.

Count pairs before writing the loop.

Practice Strategy
  • MCQ Practice — for each option, confirm loop bound is length - 1 and 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.
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 02 Adjacent-Pair Distances FRQ Practice

AP-style topic practice assessment

Start