An interactive step-by-step visualisation of the Two Pointers pattern. Watch how two pointers converge to find a solution in O(n) time.
1def two_sum(nums: list[int], target: int) -> list[int]:2 left = 03 right = len(nums) - 145 while left < right:6 total = nums[left] + nums[right]78 if total == target:9 return [left, right]10 elif total < target:11 left += 112 else:13 right -= 11415 return [] # No solution found
We have a sorted array [2, 7, 11, 15] and need to find two numbers that add up to 9.