Two Pointers
Letting the comparison steer you
On sorted input, the current value tells you which pointer to move. That is the entire technique.
Key idea
What sorting buys you
On unsorted input, knowing that two values sum to less than a target tells you nothing about where a better pair might be. On sorted input it tells you everything.
With left at the smallest remaining value and right at the largest, the sum of the pair is at its smallest when left is far left and largest when right is far right. Moving left rightward can only increase the sum. Moving right leftward can only decrease it. You now have a steering wheel.
Why it works
The steering rule
Compare the current sum to the target. Too small means the only useful move is to increase it, so advance left. Too large means decrease it, so retreat right. Equal means you are done.
The move is forced. There is never a choice about which pointer to move, which is what makes this pattern feel mechanical once you have seen it.
Why it works
What a move throws away
This is the part worth slowing down for. When the sum is too small and you advance left, you are not just moving a variable. You are permanently discarding every pair that used the old left.
Is that safe? The old left paired with right gave the largest sum that old left could ever reach, since right holds the largest remaining value. That sum was still too small. So no pair using the old left can reach the target, and discarding all of them loses nothing.
That argument is the pattern. The pointer movement is bookkeeping; the argument is the reason it is correct.
Edge cases
Duplicates and reporting
When a problem wants every qualifying pair rather than the first, equal values need care: after recording a hit, advance past the run of identical values on at least one side, or the same pair gets reported repeatedly.
When a problem returns positions rather than values, sorting destroys them. Either pair each value with its original index before sorting, or use the hash-map approach from Unit 1 instead. Choosing between those two is a real decision, not a detail.
Cost
Cost against the alternatives
Two pointers on already-sorted input is O(n) time and O(1) extra space. If you have to sort first it becomes O(n log n) time, which is usually still the better answer when the problem forbids extra memory.
The hash-map approach from Unit 1 is O(n) time and O(n) space and keeps original positions. Neither is strictly better. Say which constraint you are optimizing for and pick accordingly.
Trace the steering
Run the two-pointer search on the sorted list [2, 4, 7, 11, 15] looking for a pair summing to 18. For each step give the sum of the two ends and which pointer moves next. Write the move as left, right, or done.
This activity type is not wired up yet.
Why is the discard safe?
In the sorted two-pointer search, the sum at left and right is smaller than the target, so you advance left. What justifies never reconsidering the old left value?
Two Sum Ii Input Array Is Sorted
Read the constraints first and let them tell you what complexity is expected. Derive the approach, implement it, run the tests, and submit when it passes.