🌐
Reddit
reddit.com › r/leetcode › understanding the 2-pointer technique (with leetcode examples + my really small notes)
r/leetcode on Reddit: Understanding the 2-Pointer Technique (with LeetCode examples + my really small notes)
October 23, 2025 -

I recently wrote some short notes explaining the 2-pointer technique: one of the most common and versatile patterns in problem-solving.

The notes cover:

* Basics of how 2 pointers work

* Common strategies:

• Converging pointers

• Parallel pointers

• Trigger-based pointers

* When and why to use each

Example LeetCode problems with explanations:

• Leetcode 26. Remove Duplicates from Sorted Array

• Leetcode 5. Longest Palindromic Substring

• Leetcode 15. 3Sum

• Leetcode 167. Two Sum II

If you’ve ever found 2-pointer problems confusing, going through these step-by-step examples helps a lot, they show how different pointer behaviors emerge from one simple idea: move pointers smartly instead of brute-forcing everything.

Would love to hear how others visualize or explain this pattern.

If there’s any concept you find tricky, drop it in the comments, I can make small notes on that too.

🌐
LeetCode
leetcode.com › articles › two-pointer-technique
Two-pointer technique
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
People also ask

What are classic two pointer LeetCode problems?
The most common two pointer LeetCode problems are Two Sum II on sorted input, 3Sum, Container With Most Water, Remove Duplicates from Sorted Array, Trapping Rain Water, and Valid Palindrome. In competitive programming, Codeforces 1007A Reorder the Array uses the same sorted greedy two pointer sweep.
🌐
levelop.dev
levelop.dev › home › blog › two pointers vs sliding window: when to use each pattern
Two Pointers vs Sliding Window: When to Use Each | Levelop
How do two pointers work on sorted arrays?
On a sorted array, the two pointer technique places one pointer at the start and one at the end, then moves them based on comparisons. Because the array is sorted, a sum that is too small can only grow by advancing the left pointer, and a sum that is too large can only shrink by moving the right pointer left. Each element is visited once, giving O(n) time after an O(n log n) sort.
🌐
levelop.dev
levelop.dev › home › blog › two pointers vs sliding window: when to use each pattern
Two Pointers vs Sliding Window: When to Use Each | Levelop
Can two pointers work on unsorted arrays?
Generally no. The two pointer technique relies on sorted order for directional guarantees. On unsorted data, pointer movement gives no predictable information about whether results move toward or away from the target. Sort the array first at O(n log n) cost, or use a different technique like hash maps.
🌐
levelop.dev
levelop.dev › home › blog › two pointers vs sliding window: when to use each pattern
Two Pointers vs Sliding Window: When to Use Each | Levelop
🌐
Reddit
reddit.com › r/leetcode › two-pointer technique, an in-depth guide: concepts explained | questions to try | visuals and animations
r/leetcode on Reddit: Two-Pointer Technique, an In-Depth Guide: Concepts Explained | Questions to Try | Visuals and Animations
December 12, 2023 -

The two-pointer technique I’m referring to here involves using two pointers that start at opposite ends of an array and gradually move towards each other before meeting in the middle.

left                            right
 ↓                                ↓
 --- --- --- --- --- --- --- --- ---
| 2 | 1 | 2 | 0 | 1 | 0 | 1 | 0 | 1 |
 --- --- --- --- --- --- --- --- ---

This technique should be your go-to when you see a question that involves searching for a pair (or more!) of elements in an array that meet a certain criteria.

In this guide, we'll start by understanding how the technique produces the efficient O(n) time-complexity solutions that those questions require (answer: by eliminating pairs). I'll then provide follow-up questions for you to try, with lots of visuals and interactive animations to help along the way.

Sample Problem: Two Sum (easy)

Starting with a sorted array of integers, find a pair of numbers that sum to the given target.

Let’s walkthrough how the two-pointer technique eliminates unnecessary pairs from the search when the input array is [1, 3, 4, 6, 8, 10, 13] and target = 13

Step 1: Initialize pointers at opposite ends of array and sum the two elements together. This represents the first pair we are considering in our search.

left                     right
 ↓                         ↓
 --- --- --- --- --- ---- ----   
| 1 | 3 | 4 | 6 | 8 | 10 | 13 |     current_sum = array[left] + array[right] = 14
 --- --- --- --- --- ---- ----   

Step 2: Compare current_sum with target. Since current_sum > target, move the right pointer backwards.

To see why, notice that all other pairs that use 13 are also greater than our current_sum.So by moving the right pointer to 10, we can eliminate those unnecessary pairs from our search.

left                 right
 ↓                     ↓
 --- --- --- --- --- ---- ----
| 1 | 3 | 4 | 6 | 8 | 10 | 13 |          by moving right pointer back...
 --- --- --- --- --- ---- ----



		   21--------
                  |         | 
           17-----|-------- | 
          |       |       | | 
 --- --- --- --- --- ---- ----
| 1 | 3 | 4 | 6 | 8 | 10 | 13 |     we eliminated these pairs from our search  
 --- --- --- --- --- ---- ---- 
      |       |       |   | | |
       16-----|-------|---- | |
              |       |     | |
               19-----|------ |
                      |       |
                       23------

Step 3: Compare current_sum with target. Since current_sum < target, move the left pointer forwards. This follows similar reasoning to the step above: all other pairs that use element 1 are less than our target, so we should move our left pointer forward to eliminate those pairs.

left                 right
 ↓                     ↓
 --- --- --- --- --- ---- ----
| 1 | 3 | 4 | 6 | 8 | 10 | 13 |                move left pointer forwards
 --- --- --- --- --- ---- ----

============= TO =============

     left            right
      ↓                ↓
 --- --- --- --- --- ---- ----
| 1 | 3 | 4 | 6 | 8 | 10 | 13 |                   current_sum = 13
 --- --- --- --- --- ---- ----

Termination: Repeat process until current_sum == target, like it does here. Or, if the pointers meet at the same index, then a pair was not found.

Time Complexity: O(n). This is done in a single pass. By using the two-pointer technique, we avoid the nested for-loop required by the brute force solution.

Click here for a more in-depth breakdown of this question, including an interactive animation of the Python solution.

Try It Yourself: Follow-up Questions

  • Container With Most Water (medium)

Hint: Instead of summing the elements at each pointer, compare their values instead. Which containers can you eliminate?

Stuck? This link helps you visualize each step alongside the Python implementation.

  • 3Sum (medium)

                     i, left, right represent current triplet      

          i  left              right 
          ↓   ↓                  ↓              
         ---- ---- ---- --- --- ---                   
        | -4 | -1 | -1 | 0 | 1 | 2 |             can you use two sum?
         ---- ---- ---- --- --- ---              

                   ...

               i   left         right
               ↓    ↓            ↓              
         ---- ---- ---- --- --- ---             
        | -4 | -1 | -1 | 0 | 1 | 2 |         can you use two sum again?    
         ---- ---- ---- --- --- ---              

Hint: sort the array, iterate over each item, repeatedly apply two sum.

This link helps you visualize each step of the implementation (without showing the Python implementation)

  • Valid Triangle Number (medium)

Hint: sort the array, then use the triangle inequality, which states that if a triangle has sides of lengths a, b, and c, then all three of (1) a + b > c (2) a + c > b (3) c + b > a must be true.

                   i, left, right represent current triplet
        

        left              right  i
         ↓                  ↓    ↓              
         --- --- --- ---- ---- ----                   
        | 4 | 6 | 9 | 11 | 15 | 18 |        which triplets can you eliminate?
         --- --- --- ---- ---- ----         

This link helps you visualize each step of the implementation (without showing the Python implementation)

  • 3Sum Closest

A variation of 3Sum.

Summary

  • If a question involves searching for a pair (or more!) items in an array that meet a certain criteria, see if you can use the two-pointer technique to come up with an efficient solution.

  • The questions linked here use the two-pointer technique to eliminate unnecessary pairs from the search, producing O(n) solutions compared to the O(n2) brute-force solutions.

  • To use the technique: initialize the pointers (typically at opposite ends of the array, but not always). Look at the values at each pointer. From those values, think about how to move each pointer so that you can eliminate unecessary pairs from the search.

Bonus! Partitioning Arrays

The two-pointer technique can also be used to solve problems that involve partition arrays into different regions. For these questions, each pointer represents where the next element belonging to that region should go.

     (next 0 here!)  (next 2 here!)   
         left           right                      Sorting array of 0, 1, 2s
          ↓               ↓
 --- --- --- --- --- --- --- --- --- ---
| 0 | 0 | 1 | 1 | 2 | 1 | 1 | 2 | 2 | 2 |
 --- --- --- --- --- --- --- --- --- ---
|______||_______||__________||______|       
    0s     1s      unsorted     2s

Example: Sort Colors (medium)

Given an unsorted array nums with n integers that are either 0, 1, or 2.
Sort the array in-place in ascending order. 

Solve this problem in one-pass without any extra space.

We'll actually initialize 3 pointers:

  • left and right at opposite ends of the array. The left pointer represents the position of the next 0, and the right pointer represents the position of the next 2.

  • i at the beginning of the array. This pointer represents the current element we are trying to sort, as well as the boundary of the "ones" region.

These three pointers split our array into four regions, an unsorted region, and 0s, 1s, and 2s regions, which are all empty and not shown.

         i
        left                right
         ↓                    ↓
         --- --- --- --- --- --- 
        | 2 | 1 | 2 | 0 | 1 | 0 |
         --- --- --- --- --- --- 
        |_______unsorted________|

The idea here is that we iterate until i crosses right. At each iteration:

  • if nums[i] == 0, we swap i with the element at the left pointer, move left pointer forward and increment i

  • if nums[i] == 1, we increment i

  • if nums[i] == 2, we swap i with the element at the right pointer, move right pointer backward.

         i
        left                right
         ↓                    ↓
         --- --- --- --- --- --- 
        | 2 | 1 | 2 | 0 | 1 | 0 |        Start 
         --- --- --- --- --- ---         (empty regions not shown)
        |_______unsorted________|   
    
      
         i
        left            right
         ↓                ↓              Step 1: nums[i] == 2
         --- --- --- --- --- --- 
        | 0 | 1 | 2 | 0 | 1 | 2 |        swap i with right
         --- --- --- --- --- ---         move right pointer back
        |______unsorted_____||__|   
                              2s

              i
             left       right
              ↓           ↓             Step 2: nums[i] == 0
         --- --- --- --- --- ---
        | 0 | 1 | 2 | 0 | 1 | 2 |       swap i with left
         --- --- --- --- --- ---        move left pointer forward
        |__||___unsorted____||__|       increment i
         0s                   2s


             left i     right
              ↓   ↓       ↓             Step 3: nums[i] == 1
         --- --- --- --- --- ---
        | 0 | 1 | 2 | 0 | 1 | 2 |       increment i
         --- --- --- --- --- ---     
        |___||__||_unsorted_||__|       
          0s  1s              2s


             left i  right
              ↓   ↓   ↓                  Step 4: nums[i] == 2
         --- --- --- --- --- ---
        | 0 | 1 | 1 | 0 | 2 | 2 |       swap i with right
         --- --- --- --- --- ---        move right pointer back
        |___||__||______||______|       
          0s  1s unsorted   2s


                    ...


               left right i 
                  ↓   ↓   ↓               Termination (i > right)
         --- --- --- --- --- ---
        | 0 | 0 | 1 | 1 | 2 | 2 |         return sorted array
         --- --- --- --- --- ---
        |_______||______||______|
           0s       1s      2s

Time Complexity: Single pass, O(n).Space Complexity: O(1).

Click here a more in-depth breakdown of this question, including an interactive animation of the Python solution.

Try It Yourself

  • Move Zeroes (easy)

Not exactly the two-pointer technique described here, but good practice for using a pointer to represent a region of an array.

         what goes here? 
          nextNonZero i
              ↓       ↓  
         --- --- --- --- ---- 
        | 1 | 0 | 0 | 3 | 12 | 
         --- --- --- --- ---- 

Hint: use a pointer to represent the position of the next non-zero you find.

This link helps you visualize each step of the implementation (without showing the Python implementation)

  • Partition Array According to Given Pivot (medium)

Hint: follow a similar approach to sort colors, but copy items to a new output array to maintain relative ordering.

Summary

  • If a question calls for partitioning an array into different regions: initialize one pointer for each region you need to create.

  • Then iterate over the array and place each element in the correct position (as dictated by the pointer).

  • Move the pointer to indicate where the next element that belongs in that region should go.

I love breaking down the algorithm patterns that will help you land your next dream job in tech. There will be many more coming in the near future. If you found this guide helpful, or if there is anything you would like me to cover in the future, please leave a comment! It means a lot :)

🌐
LeetCode
leetcode.com › discuss › post › 7350824
🧠 The "Two Pointers" Cheat Sheet: Master 25+ Problems with Just ONE Pattern - Discuss - LeetCode
Stop solving problems randomly. Start recognizing patterns. Two Pointers is the most underrated pattern that appears in 100+ LeetCode problems, yet most people don't know when to use it.
🌐
Medium
medium.com › @timpark0807 › leetcode-is-easy-two-pointers-90b9b0f2eb43
Leetcode is Easy! The Two Pointer Pattern. | by Tim Park | Medium
December 23, 2019 - To begin checking, we initialize one pointer on each end, ‘left’ and ‘right’. The ‘left’ pointer will reference the left most index and the ‘right’ pointer will reference the right most index. We can check these two values to see if the letters fulfill the palindrome condition for those two indices.
🌐
Medium
medium.com › @abasaeed › understanding-two-pointers-in-python-guide-with-leetcode-tips-tricks-cd8f91ce31a9
Understanding Two Pointers in Python: Guide with LeetCode Tips & Tricks | by Abdullah Saeed | Medium
November 11, 2024 - Sorting helps because it provides order, making it easier to decide whether to move pointers inward based on conditions. However, not all two-pointer problems require sorting, such as problems involving linked lists or rearrangement tasks, where the pointers move based on conditions instead of order.
🌐
Levelop
levelop.dev › home › blog › two pointers vs sliding window: when to use each pattern
Two Pointers vs Sliding Window: When to Use Each | Levelop
3 weeks ago - The most common two pointer LeetCode problems are Two Sum II on sorted input, 3Sum, Container With Most Water, Remove Duplicates from Sorted Array, Trapping Rain Water, and Valid Palindrome.
🌐
LeetCode
leetcode.com › discuss › study-guide › 1905453 › master-in-two-pointer
🔥 Master in Two Pointer - Discuss - LeetCode
Both pointers start from the beginning but one pointer moves at a faster pace than the other one. Brute Force Approach: We can find the length of the entire linked list in one complete iteration and then iterate till half-length again.
Find elsewhere
🌐
Aman's AI Journal
aman.ai › code › two-pointers
Aman's AI Journal • Distilled • LeetCode • Two Pointers
Instead, we could move the starting index of the current subarray as soon as we know that no better could be done with this index as the starting index. We can maintain two pointers, one for the start and another for the end of the current subarray, and make optimal moves so as to keep the ...
🌐
YouTube
youtube.com › watch
Two Pointers in 7 minutes | LeetCode Pattern - YouTube
► Master DSA Patterns: https://algomaster.io/► My DSA Playlist: https://www.youtube.com/watch?v=g1nAxNKcP0M&list=PLK63NuByH5o9odyBT7nfYkHZyvGQ5oVp2GitHub rep...
Published: January 26, 2025
🌐
LeetCode Meditations
rivea0.github.io › blog › leetcode-meditations-chapter-2-two-pointers
LeetCode Meditations — Chapter 2: Two Pointers
February 29, 2024 - We initialize two pointers: left and right. left points to the start of the array, while the right points to the last element. As we loop while left is less than right, we check if they are equal. If not, we return false immediately. Otherwise, our left pointer is increased; that is, it's moved ...
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › two-pointers-technique
Two Pointers Technique - GeeksforGeeks
We use two index variables left and right to traverse from both corners. Initialize: left = 0, right = n - 1 Run a loop while left < right, do the following inside the loop ... If the sum equals the target, we’ve found the pair.
Published: February 13, 2026
🌐
LeetCode
leetcode.com › explore › learn › card › array-and-string › 205 › array-two-pointer-technique
Explore - LeetCode
A New Way to Learn. LeetCode is the best platform to help you enhance your skills, expand your knowledge and prepare for technical interviews.
🌐
DEV Community
dev.to › jayk0001 › dsa-fundamentals-two-pointers-sliding-window-from-theory-to-leetcode-practice-80f
DSA Fundamentals: Two Pointers & Sliding Window - From Theory to LeetCode Practice - DEV Community
December 5, 2025 - Two Pointers and Sliding Window are powerful algorithmic techniques that optimize array and string problems from O(n²) brute force to O(n) linear time. These patterns appear frequently in coding interviews and real-world applications, especially ...
🌐
LeetCode
leetcode.com › discuss › post › 1688903 › solved-all-two-pointers-problems-in-100-z56cn
Solved all two pointers problems in 100 days. - Discuss - LeetCode
2 Sum problem (*) https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ https://leetcode.com/problems/3sum/ https://leetcode.com/problems/4sum/ https://leetcode.com/problems/number-of-subsequences-that-satisfy-the-given-sum-condition/ https://leetcode.com/problems/two-sum-iv-input-is-a-bst/ https://leetcode.com/problems/sum-of-square-numbers/ https://leetcode.com/problems/boats-to-save-people/ https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/ https://leetcode.com/problems/3sum-with-multiplicity/
🌐
Easycodinginterview
easycodinginterview.com › home › leetcode › two pointers
Two Pointers LeetCode Problems (Python)
Master two pointers patterns with 3 interactive Python solutions. Build strong foundations for technical interviews at top companies. 1 Easy · 2 Medium · 0 Hard · #125 · Valid Palindrome LeetCode Python Solution · Two Pointers · String · Easy · #15 · 3Sum LeetCode Python Solution ·
🌐
GitHub
github.com › Chanda-Abdul › Several-Coding-Patterns-for-Solving-Data-Structures-and-Algorithms-Problems-during-Interviews › blob › main › ✅ Pattern 02: Two Pointers.md
Several-Coding-Patterns-for-Solving-Data-Structures-and-Algorithms-Problems-during-Interviews/✅ Pattern 02: Two Pointers.md at main · Chanda-Abdul/Several-Coding-Patterns-for-Solving-Data-Structures-and-Algorithms-Problems-during-Interviews
https://leetcode.com/problems/squares-of-a-sorted-array/ This is a straightforward question. The only trick is that we can have negative numbers in the input array, which will make it a bit difficult to generate the output array with squares in sorted order. An easier approach could be to first find the index of the first non-negative number in the array. After that, we can use Two Pointers to iterate the array.
Author: Chanda-Abdul
🌐
Yu He's Tech Blog
dorianhe.github.io › Two-pointer-in-Leetcode-Problems
Two-pointer in Leetcode Problems
Two-pointer is a quite common method used to solve Leetcode problems related to strings, arrays and linked lists. Usually, the problems related to strings and arrays can be solved by brute force, which basically means your code searches all ...
🌐
LeetCode
leetcode.com › tag › two-pointers › discuss
Two Pointers - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.