← All templates

Two Pointers

Pro

Use when you need to find a pair or compare elements from opposite ends of a sorted or structured input efficiently in O(n) time.

Use when you have a sorted (or structured) input and need to find or compare elements from opposite ends, converging inward in O(n) time.

Keywords for identifying Two Pointer Problems

β†’ Sorted array + find pair / triplet with a target sum
β†’ Palindrome check (compare from both ends)
β†’ Compare / merge from both ends
β†’ "Two sum" variant on sorted input
β†’ Maximize / minimize with width Γ— height
β†’ Pair heaviest with lightest (greedy pairing)

Unlock the Two Pointers template

Get the full code template and worked LeetCode examples for Two Pointers β€” plus every other pattern in the library β€” with a one-time $29 lifetime upgrade.

Upgrade to ProSee free Binary Search template β†’
Two Pointers Β· Templates Β· Grind Patterns