Week 1 · Day 2 of 42
Two Pointer: Converging
If the input is sorted and you need a pair/sum — think left+right pointers converging inward. Move the smaller side up or the larger side down.
Sign in to tick off tasks and keep your progress on every device
Two Pointers - Converging (Sorted Array Target Sum)
Tick the box when you have studied this pattern
How to recall this pattern
If the input is sorted and you need a pair/sum — think left+right pointers converging inward. Move the smaller side up or the larger side down.
- Place one pointer at the start and one at the end of a sorted array.
- If the sum is too small, move the left pointer right. If too large, move the right pointer left.
- Works because sorting gives a monotonic relationship between pointer movement and sum.
- Time: O(n), Space: O(1). Requires sorted input or sort first in O(n log n).
Solve these
Try each one before reading a solution. Name the pattern out loud first.
- Two Sum II - Input Array Is SortedConvergingMedium#167
- 3SumConvergingMedium#15
- 3Sum ClosestConvergingMedium#16
- 4SumConvergingMedium#18
Extra practice · 1 more problem
See it move
Spotted a mistake on this page?
Wrong formula, broken animation or confusing wording. Tell us and we will fix it.