StudyDSA logoStudyDSA
Data StructuresAlgorithmsBig-O NotationPractice

Command Palette

Search for a command to run...

Sign InSign Up
Sign Up

Data StructuresAlgorithmsBig-O NotationDesign PatternsSystem DesignMachine LearningPhysicsRoboticsAI Research

Two PointersFast & Slow PointersSliding WindowBinary SearchPrefix SumsRecursionSortingBacktracking1D Dynamic ProgrammingGreedyIntervalsBit OperationsDepth-First SearchBreadth-First Search2D Dynamic ProgrammingTwo-Heaps PatternDijkstra's AlgorithmA* Search AlgorithmFloyd-Warshall's AlgorithmBellman-Ford AlgorithmPrim's AlgorithmKruskal's AlgorithmTopological SortDAG Shortest Path

AlgorithmsTwo Pointers

Two Pointers

A technique using two pointers to traverse data structures efficiently.

Definition
StudyDSA

Where complexity meets clarity.
By Armas Zarra.

Topics

  • Data Structures
  • Algorithms
  • Big-O Notation
  • Robotics
  • AI Research
  • Machine Learning

Practice

  • Blind 75
  • LeetCode 75
  • NeetCode 150

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Armas Films LLC

AlgorithmsTwo Pointers

Two Pointers

A technique using two pointers to traverse data structures efficiently.

Definition

The Two Pointers technique uses two pointers to solve problems efficiently. These pointers typically move through an array or linked list in a specific way to find a solution. The setup is simple: a left pointer and a right pointer, both starting at different indices of the array. As we process the data, we move these pointers inward or outward based on conditions specific to the problem.

Types of Two Pointers

Opposite-direction pointers use a left pointer at the beginning and a right pointer at the end of an array. The pointers move toward each other based on comparing their values. If we're looking for a target sum, we move right when the sum is too large and leftwhen it's too small.


Another pattern is sliding window, where both pointers move in the same direction while maintaining a specific window size. It's useful for finding subarrays with certain properties, but this page focuses on opposite-direction pointers.

How it Works

The algorithm works by comparing values at two different positions and moving based on what we find. We start with a left pointer at the beginning (0) and a right pointer at the end (length - 1). At each step, we compare the values and move one of the pointers inward based on what we're looking for.


The pointers keep moving until they meet in the middle or we find our solution. This technique is space-efficient because it only needs two variables, and it's time-efficient since we process each element at most once. Because we don't use any extra data structures, the approach is ideal for problems that require comparing elements from different positions.

Two Pointers: Step-by-Step

1. Initialize Two Pointers

Start with two pointers, usually at opposite ends of the data structure. The most common setup is one pointer at the start (index 0) and one at the end (index length - 1) of an array. Some problems start the pointers at specific positions based on the constraints.

2. Move the Pointers

Move one or both pointers based on the problem's conditions, either toward each other or in the same direction. A common pattern is moving one pointer based on the value at the other pointer's position.

3. Compare and Process

At each step, compare the elements at the two pointers and process them according to the problem. That usually means comparing the values, processing them somehow, and updating the result.

4. Terminate

The algorithm usually ends when the pointers meet, cross each other, or a solution is found. The exact termination condition varies: pointers meeting or crossing, a specific condition being met, or the entire data structure being traversed.
Common Patterns

Opposite-direction pointers typically run inside a while loop until the pointers meet. The pattern works best when you need to compare elements from opposite ends of sorted data. In sorted arrays, moving pointers has predictable effects: moving right always gives a smaller value, while moving left gives a larger one. That predictability is what makes it easy to home in on a target.

two_pointers.py

class Solution:
    def twoPointers(self, numbers: List[int], target: int) -> List[int]:
        # Step 1: Initialize two pointers
        left, right = 0, len(numbers) - 1

        # Step 2: Move the pointers
        while left < right:
            # Step 3: Compare and process
            total = numbers[left] + numbers[right]
            if total < target:
                left += 1
            elif total > target:
                right -= 1
            # Step 4: Terminate (target found)
            else:
                return [left + 1, right + 1]

        # Step 4: Terminate (when loop ends)
        return []

Palindrome problems are another natural fit for Two Pointers because they require comparing characters from both ends of a string. By checking whether characters match as we move inward, we can validate palindromes without storing or comparing the entire string up front.

Best Practices

Two Pointers is one of the most reliable patterns for solving array, string, and linked list problems efficiently. Here are a few tips for implementing it well and avoiding the common pitfalls:


Two Pointers is the right tool for problems involving sorted arrays, strings, or linked lists where you need to find pairs with certain characteristics. It's especially effective when you need to compare elements from different positions in the data structure.

Practice Problems

The two-pointers technique converts a lot of "try every pair" brute force into clean linear scans. Work through these to internalize convergent (left/right) pointers, fast/slow write pointers, and the trickier variants where the two-pointer logic stitches into sliding window or DP:


Two Pointers
Completed
Title
Status
Notes
Solution
Valid Palindrome
Easy
Reverse String
Easy
Squares of a Sorted Array
Easy
Move Zeroes
Easy
Two Sum II - Input Array Is Sorted
Medium
3Sum
Medium
Container With Most Water
Medium
Sort Colors
Medium
Trapping Rain Water
Hard
Subarrays with K Different Integers
Hard
Minimum Number of Removals to Make Mountain Array
Hard
Get the Maximum Score
Hard