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.
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.
Shifting and pair vocabulary.
Combined array manipulation.
- 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)withi < j. - Double counting — counting (a, b) and (b, a) as different pairs.
- Pair count — total of
n*(n-1)/2for full enumeration.
How shifting and pairs are tested.
Direction and pair-uniqueness dominate.
- 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.
Count pairs summing to 10.
Nested loop with j > i.
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.
Shift and pair pitfalls.
Most overwrite data or double-count.
- 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.
Shift and pair patterns.
Direction and start index in one place.
Shift & Pair Reference
AP Quick Reference| Task | Direction | Detail |
|---|---|---|
| Shift right | Backward | Preserves data. |
| Shift left | Forward | Preserves data. |
| Pair count | Nested loops | j = i + 1. |
| Total pairs | n*(n-1)/2 | Sanity bound. |
How to master shifting and pairs.
Direction for shifting; j = i + 1 for pairs.
- 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.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 07 Topic 10 In-Place Shifting and Pair Counting FRQ Practice
AP-style topic practice assessment