Two Pointer Problems Pattern

Himanshu KumarHimanshu Kumar
4 min read

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

TypeWhen to UseExample Problems
Opposite DirectionFinding pairs, palindromes, max/min valuesTwo Sum (Sorted), Palindrome Check, Container with Most Water
Same DirectionMerging lists, longest/shortest subarraysMerging Two Sorted Arrays, Longest Subarray โ‰ค K
Sliding WindowFinding the longest/shortest substring/subarrayLongest Substring Without Repeating
Split & Merge (Divide & Conquer)Breaking arrays into halves, recursive mergingMerge Sort, Median of Two Sorted Arrays
Running from Start (Two Arrays)Merging sorted arrays, finding intersectionsIntersection of Two Sorted Arrays
Slow & Fast PointersLinked list cycles are in the middle of the list.Floyd's Cycle Detection.
0
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.