- Overview
- Key Concepts
- Learning Resources
- Practice Problems
- Implementation Examples
- Time Complexity Analysis
- Common Patterns
Master the Two Pointer Pattern with this comprehensive PDF guide! Learn to identify the right problems in seconds and streamline your coding solutions. Enhance your algorithmic thinking today. The Two Pointer technique is a fundamental algorithmic pattern used to solve array and string problems efficiently. Instead of using nested loops (O(nΒ²)), this technique reduces time complexity to O(n) by using two pointers that traverse the data structure strategically.
- Array/String manipulation problems
- Searching pairs with specific conditions
- Palindrome checking
- Sliding window problems
- Merge operations on sorted arrays
- Opposite Direction - Start from both ends, move towards center
- Same Direction - Both pointers start from same position
- Fast & Slow - Different speeds for cycle detection
- Link: Educative Two Pointers Pattern
- What's Great: Interactive visual explanations, structured learning path
- Best For: Systematic interview preparation
- Link: GfG Two Pointers Guide
- What's Great: Detailed explanations, multiple examples, code implementations
- Best For: Understanding fundamentals and basic implementations
- Cost: Free
Essential Reads:
-
"LeetCode is Easy! The Two Pointer Pattern" by Tim Park
- Recognition techniques and pattern variations
- Problem classification strategies
-
"Master the Two Pointer Technique" by John Kamau
- Time complexity analysis and optimization techniques
- Real-world problem examples
- Link: LeetCode Two Pointers Problems
- Problems Count: 100+ curated problems
- Difficulty: Easy to Hard
- Features: Community solutions, discussion forums
- Link: HackerRank Practice
- What's Great: Gradual difficulty progression
- Best For: Building confidence through structured practice
Problem | Platform | Difficulty | Pattern Type |
---|---|---|---|
Two Sum | LeetCode #1 | Easy | Opposite Direction |
Valid Palindrome | LeetCode #125 | Easy | Opposite Direction |
Remove Duplicates | LeetCode #26 | Easy | Same Direction |
Problem | Platform | Difficulty | Pattern Type |
---|---|---|---|
3Sum | LeetCode #15 | Medium | Opposite Direction |
Container With Most Water | LeetCode #11 | Medium | Opposite Direction |
Longest Substring Without Repeating | LeetCode #3 | Medium | Sliding Window |
Problem | Platform | Difficulty | Pattern Type |
---|---|---|---|
Trapping Rain Water | LeetCode #42 | Hard | Opposite Direction |
Minimum Window Substring | LeetCode #76 | Hard | Sliding Window |
Linked List Cycle II | LeetCode #142 | Medium | Fast & Slow |
def two_sum_sorted(arr, target):
left, right = 0, len(arr) - 1
while left < right:
current_sum = arr[left] + arr[right]
if current_sum == target:
return [left, right]
elif current_sum < target:
left += 1
else:
right -= 1
return [-1, -1]
def remove_duplicates(arr):
if not arr:
return 0
write_index = 1
for read_index in range(1, len(arr)):
if arr[read_index] != arr[read_index - 1]:
arr[write_index] = arr[read_index]
write_index += 1
return write_index
def has_cycle(head):
if not head:
return False
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False
Approach | Time Complexity | Space Complexity | Use Case |
---|---|---|---|
Brute Force | O(nΒ²) | O(1) | Simple nested loops |
Two Pointers | O(n) | O(1) | Optimized linear scan |
Hash Map | O(n) | O(n) | When two pointers not applicable |
Use Two Pointers When:
- Array/string is sorted or can be sorted
- Looking for pairs/triplets with specific sum
- Need to reverse or palindrome check
- Sliding window maximum/minimum problems
- Merge two sorted sequences
Avoid Two Pointers When:
- Need to track multiple elements simultaneously
- Random access patterns required
- Complex state management needed
Found a great resource or want to add more examples?
- Fork this repository
- Create a feature branch (
git checkout -b feature/new-resource
) - Add your resource with proper description and links
- Test all links and verify content quality
- Submit a pull request with detailed description
- Verify resource quality and accuracy
- Include brief description of what makes it valuable
- Categorize appropriately (beginner/intermediate/advanced)
- Follow existing markdown formatting
- Read GeeksforGeeks comprehensive guide
- Understand basic patterns and implementations
- Solve 10-15 easy problems on LeetCode
- Study Educative.io interactive course
- Focus on different pattern variations
- Solve 20-30 medium problems
- Review USACO competitive programming guide
- Read Medium articles for deeper insights
- Tackle hard problems and optimization challenges
Feel free to open an issue or reach out if you:
- Have questions about specific problems
- Want to suggest additional resources
- Found broken links or outdated information
- Need clarification on any pattern or concept
Happy Coding! π
Last Updated: 9/3/2025
Maintained by: Nozibul Islam