Two Pointers
Authors: Darren Yao, Qi Wang, Ryan Chou
Contributors: Aadit Ambadkar, Juheon Rhee
Iterating two monotonic pointers across an array to search for a pair of indices satisfying some condition in linear time.
Resources
Resources | ||||
---|---|---|---|---|
CPH | solutions to the problems above | |||
IUSACO | above + mention of max subarray sum | |||
CF EDU | video explanation of two pointers |
Two Pointers
The two pointers method iterates two pointers across an array, to track the start and end of an interval. It can also be used to track two values in an array as shown in CPH's 2SUM solution.
Focus Problem – try your best to solve this problem before continuing!
View Internal SolutionSolution - Books
We want to find the longest contiguous segment of books that can be read within minutes.
To accomplish this, we can define and to represent the beginning and end of the segment. Both will start at the beginning of the array. These numbers can be thought of as pointers, hence the name "two pointers."
For every value of in increasing order, let's increase until the total time for the segment is maximized while being less than or equal to .
will store the maximum value of (segment size) that we have encountered so far.
After incrementing by one, the time used decreases, hence the right pointer never has to move leftwards. Thus:
Since both pointers will move at most times, the overall time complexity is .
As an example, consider the first case in the sample inputs:
Left Pointer | ||||
3 | 1 | 2 | 1 | |
Right Pointer |
We can move the right pointer to index :
Left Pointer | ||||
3 | 1 | 2 | 1 | |
Right Pointer |
The sum of the values in this range is , and there are values. So, the current maximum segment length is . By incrementing the left pointer by , we can subtract from the sum of values to get . The array then looks like:
Left Pointer | ||||
3 | 1 | 2 | 1 | |
Right Pointer |
Now, we can move the right pointer to the end. This makes the sum of values and the length of the segment equal to . So, is now .
Left Pointer | ||||
3 | 1 | 2 | 1 | |
Right Pointer |
Since the right pointer has reached the end of the array, we are done at this point. This leaves us with .
Here's an animation of the above example:
Pro Tip
You can visualize these pointers as maintaining a sliding window of books for this problem.
Implementation
C++
Code Snippet: C++ Short Template (Click to expand)int main() {setIO();int n, t, ans = 0;cin >> n >> t;vi books(n);for (int i = 0; i < n; i++) { cin >> books[i]; }int left = 0, right = 0, cur = 0;
Java
import java.io.*;import java.util.*;public class Books {public static void main(String[] args) {Kattio io = new Kattio();int n = io.nextInt();int t = io.nextInt();int[] books = new int[n];
Python
n = int(input())t = int(input())books = [int(input()) for _ in range(n)]right = 0left = 0cur = 0ans = 0
Problems
Status | Source | Problem Name | Difficulty | Tags | |
---|---|---|---|---|---|
CSES | Easy | Show Tags2P, Sorting | |||
CSES | Easy | Show TagsSliding Window | |||
CSES | Easy | Show Tags2P, Sorting | |||
Silver | Easy | Show Tags2P, Sorting | |||
CF | Easy | Show Tags2P | |||
CF | Easy | Show Tags2P | |||
CF | Normal | Show Tags2P | |||
Silver | Normal | Show Tags2P, Sorting | |||
Silver | Normal | Show Tags2P, Sorting | |||
CF | Normal | Show Tags2P |
Quiz
What is the two pointers algorithm?
Module Progress:
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!