--- id: 2P title: "Two Pointers" author: Darren Yao prerequisites: - - Silver - Sorting --- **Two pointers** refers to iterating two monotonic pointers across an array to search for a pair of indices satisfying some condition in $O(n)$ time. ## Tutorials - Intro to USACO 14.1 - CPH 8.1 ## Problems - CSES - [Sum of Two Values](https://cses.fi/problemset/task/1640) - USACO - [Silver - Diamond Collector](http://usaco.org/index.php?page=viewproblem2&cpid=643) - sort and then use 2P - [Silver - Paired Up](http://usaco.org/index.php?page=viewproblem2&cpid=738) - sort and then use 2P - [Gold - Haybale Feast](http://usaco.org/index.php?page=viewproblem2&cpid=767) - just 2P with additional set - CF - [Books](https://codeforces.com/problemset/problem/279/B) - [Cellular Network](http://codeforces.com/problemset/problem/702/C) [](48) - [USB vs. PS/2](http://codeforces.com/problemset/problem/762/B) [](53) - [K-Good Segment](http://codeforces.com/problemset/problem/616/D) [](53) - [(Long Title)](http://codeforces.com/problemset/problem/814/C) [](54) - [Jury Meeting](http://codeforces.com/problemset/problem/853/B) [](90) ## Extensions The two pointers technique can be extended to the two dimensional sliding window algorithm. If you're looking for a more challenging problem using 2-D sliding window, see [USACO Plat Fort Moo](http://usaco.org/index.php?page=viewproblem2&cpid=600).