PatternsTwo Pointers Pattern

Two Pointers Pattern

Master the art of using two pointers to solve array and string problems efficiently. Perfect for sorted arrays, palindromes, and pair-finding problems.

44 Problems
5-6 Hours
Easy to Hard

When to Use Two Pointers Pattern

Sorted Arrays

When dealing with sorted arrays and need to find pairs or triplets

Palindrome Problems

Checking if strings or arrays form palindromes

Pair Sum Problems

Finding pairs that meet specific criteria (sum, difference, etc.)

Container/Water Problems

Optimizing area calculations with height constraints

Key Insights & Tips

Time Complexity

Usually O(n) compared to O(n²) brute force approach

Space Efficiency

O(1) extra space - no additional data structures needed

Common Mistake

Forgetting to handle duplicate elements properly

Pro Tip

Sort the array first if not already sorted

Code Examples

Basic Two Pointers Template

Standard template for two pointers approach on sorted arrays

Time: O(n)Space: O(1)
function twoPointers(arr: number[], target: number): number[] {
    let left = 0;
    let right = arr.length - 1;
    
    while (left < right) {
        const sum = arr[left] + arr[right];
        
        if (sum === target) {
            return [left, right];
        } else if (sum < target) {
            left++;
        } else {
            right--;
        }
    }
    
    return [-1, -1]; // Not found
}

Palindrome Check

Using two pointers to check if a string is a palindrome

Time: O(n)Space: O(1)
function isPalindrome(s: string): boolean {
    let left = 0;
    let right = s.length - 1;
    
    while (left < right) {
        // Skip non-alphanumeric characters
        while (left < right && !isAlphaNumeric(s[left])) {
            left++;
        }
        while (left < right && !isAlphaNumeric(s[right])) {
            right--;
        }
        
        if (s[left].toLowerCase() !== s[right].toLowerCase()) {
            return false;
        }
        
        left++;
        right--;
    }
    
    return true;
}

function isAlphaNumeric(char: string): boolean {
    return /[a-zA-Z0-9]/.test(char);
}

Three Sum Problem

Finding triplets that sum to zero using two pointers

Time: O(n²)Space: O(1)
function threeSum(nums: number[]): number[][] {
    const result: number[][] = [];
    nums.sort((a, b) => a - b);
    
    for (let i = 0; i < nums.length - 2; i++) {
        // Skip duplicates for first element
        if (i > 0 && nums[i] === nums[i - 1]) continue;
        
        let left = i + 1;
        let right = nums.length - 1;
        
        while (left < right) {
            const sum = nums[i] + nums[left] + nums[right];
            
            if (sum === 0) {
                result.push([nums[i], nums[left], nums[right]]);
                
                // Skip duplicates
                while (left < right && nums[left] === nums[left + 1]) left++;
                while (left < right && nums[right] === nums[right - 1]) right--;
                
                left++;
                right--;
            } else if (sum < 0) {
                left++;
            } else {
                right--;
            }
        }
    }
    
    return result;
}

Your Progress

Problems Solved2/25
8% Complete23 remaining

Practice Problems

View All

Two Sum

easy

Three Sum

medium

Four Sum

medium

Remove Duplicates from Sorted Array

easy

Container with Most Water

medium

Asked by Companies

GoogleAmazonMicrosoftFacebookAppleNetflixUberLinkedIn

Next Pattern

Ready for the next challenge?

Sliding Window