Two Pointer Problems Pattern

1. Opposite Direction (Two-pointer from Ends)
โ Use When:
You need to find pairs in a sorted array.
You need to check if a string/array is a palindrome.
You are finding an optimal solution between two boundaries (like maximum area, min/max sum).
๐น Examples:
Two Sum (Sorted Array) โ Find if two numbers add up to a target.
Checking Palindrome โ "racecar" โ Yes, "hello" โ No
Container with Most Water โ Find the max area between two vertical lines.
๐ How to Apply:
Start with one pointer at the beginning and one at the end.
Move pointers towards each other based on conditions.
2. Same Direction (Two-pointer moving forward)
โ Use When:
You need to merge two sorted arrays/lists.
You need to find the longest or shortest subarray/substring.
You are iterating through a sorted structure while maintaining constraints.
๐น Examples:
Merging Two Sorted Arrays โ arr1 = [1,3,5], arr2 = [2,4,6] โ [1,2,3,4,5,6]
Finding Longest Subarray with Sum โค K โ Find the longest contiguous subarray where sum โค K.
๐ How to Apply:
Use two pointers that move forward while checking conditions.
Advance one or both pointers as needed.
3. Sliding Window (Variable Size)
โ Use When:
You need to find the longest/shortest subarray that satisfies a condition.
You need to find the smallest window that contains certain elements.
You are working with substring/subarray optimization problems.
๐น Examples:
Longest Substring Without Repeating Characters โ "abcabcbb" โ "abc"
Smallest Subarray with Sum โฅ K โ [2,3,1,2,4,3], K=7 โ [4,3]
๐ How to Apply:
Start with one pointer expanding the window.
Move the other pointer to shrink when constraints are exceeded.
4. Split & Merge (Divide & Conquer)
โ Use When:
You need to divide an array into two halves and solve recursively.
You are implementing Merge Sort or Quick Sort.
The problem involves breaking down and recombining.
๐น Examples:
Merge Sort (Recursive Merge Step)
Find the Median of Two Sorted Arrays โ Divide both arrays and find the median.
๐ How to Apply:
Recursively split an array into two halves.
Use merge logic (often a two-pointer technique) to recombine sorted halves.
5. Running from the Beginning of Two Arrays (Merging Two Arrays)
โ Use When:
You need to merge sorted lists/arrays efficiently.
You are finding the intersection/union of two sorted lists.
The problem involves scanning two lists in a linear pass.
๐น Examples:
Merging Two Sorted Arrays โ arr1 = [1,3,5], arr2 = [2,4,6]
Intersection of Two Sorted Arrays โ arr1 = [1,2,3,4], arr2 = [2,4,6] โ [2,4]
๐ How to Apply:
Use two pointers, one on each list, moving forward.
Compare elements at both pointers and take the smaller one.
6. Slow & Fast Pointers (Floydโs Tortoise and Hare)
โ Use When:
You need to find the middle of a linked list.
You need to detect a cycle in a linked list.
The problem involves skipping elements at different speeds.
๐น Examples:
Finding the Middle of a Linked List โ head โ [1,2,3,4,5] โ middle = 3
Cycle Detection in a Linked List (Floydโs Algorithm)
๐ How to Apply:
Use one slow pointer (moves 1 step) and one fast pointer (moves 2 steps).
There is a cycle if the fast pointer catches up to the slow pointer.
If you want to find the middle, stop when the fast pointer reaches the end.
Summary Table
Type | When to Use | Example Problems |
Opposite Direction | Finding pairs, palindromes, max/min values | Two Sum (Sorted), Palindrome Check, Container with Most Water |
Same Direction | Merging lists, longest/shortest subarrays | Merging Two Sorted Arrays, Longest Subarray โค K |
Sliding Window | Finding the longest/shortest substring/subarray | Longest Substring Without Repeating |
Split & Merge (Divide & Conquer) | Breaking arrays into halves, recursive merging | Merge Sort, Median of Two Sorted Arrays |
Running from Start (Two Arrays) | Merging sorted arrays, finding intersections | Intersection of Two Sorted Arrays |
Slow & Fast Pointers | Linked list cycles are in the middle of the list. | Floyd's Cycle Detection. |
Subscribe to my newsletter
Read articles from Himanshu Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Himanshu Kumar
Himanshu Kumar
Tech enthusiast and problem-solver with a flair for creating impactful digital experiences. always pushing boundaries, solving real-world problems, and building solutions that make a difference.