How to use the Two Pointer Technique Visualizer
- 1. Pick a problemPair sum, remove duplicates, container with most water, palindrome or reverse.
- 2. Enter inputNumbers separated by commas, or text for the palindrome check, plus a target if needed.
- 3. ReplayEach step shows where L and R are and why one of them moves.
Worked example: pair with a target sum
For the sorted array 1, 2, 4, 6, 8, 9, 14, 15 and a target of 13, L starts at 1 and R at 15. 1 + 15 = 16 is too big, so R moves left; 1 + 14 = 15 is still too big; 1 + 9 = 10 is too small, so L moves right; 2 + 9 = 11 is too small; 4 + 9 = 13 is the answer, at indexes 2 and 5, found in 5 steps. Checking every pair would take 28 comparisons for 8 numbers.
Why it works
When the sum is too big, the right value can't pair with anything to the right of L (those are even bigger), so it's safe to drop. When the sum is too small, the left value is ruled out the same way. Each step removes one candidate for good, so the whole search is O(n) with O(1) extra space. Container with most water uses the same idea: moving the taller line can never give a bigger area, so the shorter line moves.
Variations
- Opposite ends moving inwards: pair sum, palindromes, reversing, 3Sum after sorting.
- Slow and fast pointers in the same direction: removing duplicates in place, where a write pointer W trails the reader.
- Fast and slow on linked lists: finding the middle or a cycle.
Frequently asked questions
Does pair sum work on unsorted arrays?
No. Sort first, or use a hash map for O(n) without sorting.
How big can the input be?
Up to 30 numbers, so every cell fits on screen.
Is the palindrome check case-sensitive?
No. Text is lower-cased and only letters and digits are compared.
Is the two pointer visualizer free?
Yes. Everything in the two pointer visualizer works without signing up. The first time you ask for a result, we ask once for your name and WhatsApp number; after that the two pointer visualizer and every other Uncodemy tool open straight away.