In problems where we deal with sorted arrays (or LinkedLists) and need to find a set of elements that fulfill certain constraints, the Two Pointers approach becomes quite useful. The set of elements could be a pair, a triplet or even a subarray.
There is no "Pseudo code" for two pointer problems, the things we need to figure out while using the approach:
- different kinds of the two pointers:
- left pointer starts from index 0 and right pointer starts from the last index
- one pointer is slow and one pointer is fast
- when to increase/decrease the two pointers