Skip to content

Two pointers

Two pointers pattern is where two pointers iterate throught a data astructure while one or both hits a certain condition. Pointers can start from two ends of the data structure and move closer.

Simply put a for loop in a for loop is a two pointer pattern. Usually has a O(n^2) time complexity.

Problems of this sort are dealing with sorted arrays (or linked lists) with the need to find a set of elements that fulfill certain condition.