Overview
Two-pointer problems are a class of problems that can be solved using two pointers moving through the dataset, typically a linear data structure such as an array or a linked list. These problems can often be solved more efficiently with two pointers than would be possible using only one.
Do note that Two Pointers can be iterated using either while
and for
loops. Also, you can decide whether left pointer
or right pointer
should be placed in the outer loop based your coding styles.
Typically there are 4 Types of Two Pointer Algorithms.
- Collision Pointers
- Forward Pointers (a.k.a Sliding Window)
- Parallel Pointers
- Fast Slow Pointers (a.k.a Cyclic Pointers)
Let’s have a look into these in the next chapter!