Two Ponters Technique
-
Two pointers = using two indices that move across an array/string to solve a problem efficiently.
-
It usually reduces complexity from O(n²) to O(n).
There are 5 major types of two-pointers patterns.
1️⃣ Opposite Direction Pointers (Left + Right)
Array: [4, 7, 9, 12, 15, 20]
↑ ↑
l r
Opposite-direction pointers
Used mostly when:
-
Array/string is sorted
-
You are looking for a pair satisfying some condition
Example problems:
-
Pair Sum (Two Sum in Sorted Array)
-
Container With Most Water
-
Valid Palindrome
-
Reverse a string / linked list
Template:
int l = 0, r = n - 1;
while (l < r) {
// Check condition
if (arr[l] + arr[r] == target) return true;
if (arr[l] + arr[r] < target) l++;
else r--;
}
2️⃣ Sliding Window (Two Pointers in same direction)
Array: [3, 5, 7, 1, 4, 9]
↑--------↑
l r
Sliding window
Both l and r move forward.
Used when:
- You need a window growing/shrinking
You want:
-
Largest substring without repeating characters
-
Minimum window substring
-
Subarray sum constraints
-
Fixed-size or variable-size windows
Template:
int l = 0;
for (int r = 0; r < n; r++) {
// expand window by including arr[r]
while (window violates condition) {
// shrink from left
l++;
}
// update answer
}
For more on Sliding Window Click Here
3️⃣ Fast & Slow Pointers (Tortoise & Hare)
Used for:
-
Detecting cycles in linked lists
-
Finding middle of linked list
-
Removing Nth node from end
-
Checking if number is happy
Template:
ListNode* slow = head;
ListNode* fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
4️⃣ Same Direction but With Gap / K-distance Pointers
Used for:
-
Removing duplicates in sorted array
-
Merging sorted arrays
-
Comparing substrings
-
K-distance apart comparisons
Example:
Move valid elements forward:
int i = 0;
for (int j = 0; j < n; j++) {
if (arr[j] != arr[i]) {
i++;
arr[i] = arr[j];
}
}
5️⃣ Two Pointers on Two Arrays
Used for:
-
Merging sorted arrays (merge sort)
-
Intersection of arrays
-
Comparing elements of two sequences simultaneously
Template:
int i = 0, j = 0;
while (i < n && j < m) {
if (a[i] == b[j]) { ... i++; j++; }
else if (a[i] < b[j]) i++;
else j++;
}
⭐ Must Do Problems
Level 1 — Basics
1. Leetcode 125. Valid Palindrome
- For my Leetcode Solution link click Efficient O(n) palindrome check using two pointers and character normalization
2. Leetcode 26. Remove Duplicates from Sorted Array
- For my Leetcode Solution link click Remove Duplicates from Sorted Array — In-place Two-Pointer (O(n))
Level 2 — Medium
1. Leetcode 167. Two Sum II - Input Array Is Sorted
- For my Solution click PDF Solution
2. Leetcode 15. 3Sum
- For my Leetcode Solution link click Three-Sum Using Sorting + Two Pointers (C++, O(n²))
3. Leetcode 11. Container With Most Water
- For my Leetcode Solution link click Container With Most Water — Two-Pointer Optimized Solution (O(n))
4. Leetcode 31. Next Permutation
- For my Leetcode Solution link click Next Permutation — Pivot + Swap + Reverse (O(n) Optimal Approach)
Level 3 — Hard
1. Leetcode 42. Trapping Rain Water
-
For my Leetcode Solution link click Trapping Rain Water — Prefix/Suffix Max Approach (O(n) Time, O(n) Space)
-
My Leetcode Optimal Solution link click Trapping Rain Water — Optimal O(1)-Space Two-Pointer Approach