Algorithmsrecursion two pointer
Recursion Two Pointer — Solve Like Subproblems
TT
Testlaa Team
May 14, 2026•1 min read
Recursion two-pointer style: try a move at one end, recurse on the rest, backtrack if needed (classic for exhaustive search with pruning).
Try this in Python
def two_sum_sorted_all(nums: list[int], target: int, start: int) -> list[tuple[int, int]]:
"""Enumerate unique pairs >= start (educational)."""
out: list[tuple[int, int]] = []
i, j = start, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
out.append((i, j))
i += 1
j -= 1
elif s < target:
i += 1
else:
j -= 1
return out
print(two_sum_sorted_all([1, 1, 2, 3, 4], 5, 0))
Key takeaways
- Pure iterative here; recursion variant appears in DFS on grids.
- Mental model: recursion explores choices, pointers scan.
- Avoid exponential blow-up—add pruning.
Tags:
Two pointersPythonStudents
