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. |