AI engineering glossary · Data structures algorithms
What is two pointers?
Two pointers: A technique that moves two indices through a sequence, often from both ends or at different speeds, to solve a problem in one pass instead of nested loops. It turns many O(n^2) checks into O(n).
Two pointers: an example
To find two numbers summing to 10 in the sorted list [1, 3, 4, 6, 9], move a left pointer up when the sum is too small and a right pointer down when too large.
Related terms
More Data structures algorithms terms
- Union-find
- Weighted graph
- Worst-case analysis
- A* search
- Abstract data type
- Adjacency list
- Adjacency matrix
- Algorithm
Where you learn it
Two pointers is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.