3Sum
Fix one number, then run two pointers over the rest for its negative.
Step through it →Two pointers start at both ends of a sorted list and walk inward, and every step rules out every pair one number could still make.
When the sum is too big, the right number is too big even for the smallest partner left, so no pair can use it. When it's too small, the same goes for the left number. Each move drops one number for good.
Each input here has tripped up a version of this code. The stage runs the one that gets it right.
A loop that runs while left ≤ right pairs 3 with itself and answers 3 + 3. This one stops when the pointers meet.
Fix one number, then run two pointers over the rest for its negative.
Step through it →Move the shorter wall. Keeping it can only lose width.
Step through it →Move the lower side. Its water level is already decided.
Step through it →Compare the two ends, skipping anything that isn't a letter or digit.
Step through it →The same algorithm in plain Python. The lit line is the one the current frame came from.
def two_sum(numbers, target): left, right = 0, len(numbers) - 1 while left < right: total = numbers[left] + numbers[right] if total == target: return [left + 1, right + 1] if total > target: right -= 1 else: left += 1 return []