Two Pointer Technique
a popular programming technique used to solve problems involving arrays or lists efficiently. It involves using two pointers (or indices) to traverse the data structure and is commonly used in problems related to searching, sorting, and subarray operations. It Typically can reduces the time complexity from $O(n^2)$ to $O(n)$ for many problems. - ChatGPT
Example
std::unique - use this 2 pointer techniques
template<class ForwardIt>
ForwardIt unique(ForwardIt first, ForwardIt last)
{
if (first == last)
return last;
ForwardIt result = first;
while (++first != last)
if (!(*result == *first) && ++result != first)
*result = std::move(*first);
return ++result;
}
see also
- LeetCode - Two pointer problem.
Written on November 17, 2024, Last update on April 5, 2025
algorithm
array
sort
c++