01
Overview

Shifting elements and counting matching pairs.

Two related operations: moving elements to make room, and tallying pairs that meet a condition.

In-place shifting moves elements left or right within the same array — usually to insert or delete without allocating new memory. Pair counting tallies pairs (i, j) that share some property.

Shifting requires direction-aware loops: shift right uses a backward loop; shift left uses a forward loop. Pair counting uses nested loops with j > i to avoid double counting.

On the AP exam, shifting appears in array-modification FRQs; pair counting appears in "how many pairs" or "common-difference" problems.

02
Core Concept

Shifting and nested-pair templates.

Direction matters for shifting; j > i matters for pairs.

Shift right (insert space)

// Shift positions [start..end-1] right by 1
for (int i = end; i > start; i--) {
    arr[i] = arr[i - 1];
}
arr[start] = newValue;

Backward iteration prevents overwriting unread values.

Shift left (delete)

// Shift positions [start+1..end] left by 1 (effectively remove arr[start])
for (int i = start; i < end; i++) {
    arr[i] = arr[i + 1];
}

Forward iteration; the last position becomes a duplicate or sentinel.

Counting matching pairs

public int pairsSummingTo(int[] arr, int target) {
    int count = 0;
    for (int i = 0; i < arr.length; i++) {
        for (int j = i + 1; j < arr.length; j++) {
            if (arr[i] + arr[j] == target) count++;
        }
    }
    return count;
}

Starting inner loop at j = i + 1 ensures each pair counted exactly once.

03
Key Vocabulary

Shifting and pair vocabulary.

Combined array manipulation.

Vocabulary
  • In-place shift — moving elements without a new array.
  • Right shift — moving toward higher indices.
  • Left shift — moving toward lower indices.
  • Pair — two indices (i, j) with i < j.
  • Double counting — counting (a, b) and (b, a) as different pairs.
  • Pair count — total of n*(n-1)/2 for full enumeration.
04
AP Exam Focus

How shifting and pairs are tested.

Direction and pair-uniqueness dominate.

Exam Focus
  • Shift right backward. Forward overwrites data.
  • Shift left forward. Backward overwrites data.
  • Pair loop starts at i + 1. Prevents double counting.
  • Skip i == j. Self-pairs aren't pairs.
05
Worked Example

Count pairs summing to 10.

Nested loop with j > i.

Worked Example AP-style reasoning

Input: [1, 9, 3, 7, 5, 5], target 10.

Trace pairs:

  • (0,1): 1+9=10 ✓
  • (2,3): 3+7=10 ✓
  • (4,5): 5+5=10 ✓

Other pairs don't sum to 10. Count: 3.

06
Common Mistakes

Shift and pair pitfalls.

Most overwrite data or double-count.

Watch Out
  • Wrong shift direction. Right shifts must go backward.
  • Inner loop starts at 0. Double-counts pairs.
  • Inner loop includes j == i. Self-pairs counted.
  • Off-by-one in shift range. Leaves a stale value or overwrites.
07
Reference Table

Shift and pair patterns.

Direction and start index in one place.

Shift & Pair Reference

AP Quick Reference
TaskDirectionDetail
Shift rightBackwardPreserves data.
Shift leftForwardPreserves data.
Pair countNested loopsj = i + 1.
Total pairsn*(n-1)/2Sanity bound.
08
Practice Tip

How to master shifting and pairs.

Direction for shifting; j = i + 1 for pairs.

Practice Strategy
  • MCQ Practice — for shifting, verify direction; for pairs, verify j = i + 1.
  • Java Lab — implement shiftRight, shiftLeft, pairsSummingTo. Test boundaries.
  • FRQ Practice — when "how many pairs" appears, use nested loops with the start-after-i trick.
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 10 In-Place Shifting and Pair Counting FRQ Practice

AP-style topic practice assessment

Start