Decide which end to advance
Updated
The decision reads a[L] and a[R], usually their sum. Move the end whose value you have consumed. Do not move both unless the algorithm says so.
What to focus on
Exactly one of L or R changes per decision. After the move, name the new indices. Too-small sum advances L. Too-large sum advances R. A hit does not discard an end.
Worked examples
sum too large
a = [1, 4, 9], target=8, L=0, R=2- L=0, R=2
- 1+9=10 > 8
- advance R
Result: L=0, R=1
sum too small
a = [1, 4, 9], target=13, L=0, R=2- L=0, R=2
- 1+9=10 < 13
- advance L
Result: L=1, R=2
equal
a = [1, 4, 9], target=10, L=0, R=2- L=0, R=2
- 1+9=10 hit
- do not pick an end to discard
Result: L=0, R=2 · pair found
Try it
Write decideEnd(a, L, R, target). a is a sorted array of integers. L is the left index. R is the right index. target is the pair sum you want. Move L if the sum is too small, R if too large, neither on a hit. Print which pointer moved and the new indices. On sorted [1, 4, 9] from L=0, R=2: target 8 moves R, target 13 moves L, target 10 hits and stays.
a is sorted non-decreasing, length ≥ 2. Start at L = 0, R = n-1.