Normally, when facing problems that mention a sorted array
or sorted list
, or an array that you can sort and keep the order, and you need to find a set of elements that meet some condition, two pointers
is a technique you can consider using.
The set of elements could be a pair, a triplet, or even a subarray.
For instance, these following problems can be solved using two pointers:
You can check more problems that can be solved using two pointers here.