The Two Pointer Technique: A Complete Guide

The two pointer technique is one of the most common and powerful patterns used in competitive programming, data structures, and algorithms. It helps solve problems that involve searching, sorting, or traversing arrays, strings, or linked lists in an efficient way.

Core

At its core, the idea is simple, Instead of brute-forcing through every possible pair (which usually takes O(n²) time), we strategically move two pointers to find solutions in O(n) or O(n log n).

When to Use the Two Pointer Technique

You should consider using two pointers when:

  • Sorted arrays or strings are involved: Many problems with sorted data can be solved in linear time with two pointers.
  • You need to find a pair or subarray that meets a condition: Examples: target sum, closest pair, longest substring, sliding window problems.
  • You want to avoid nested loops: Instead of iterating twice, two pointers can achieve the same result with one pass.

Types of Two Pointer Approaches

Opposite Ends (Converging Pointers)

Example Problem: Find if a sorted array has two numbers that sum to a target.

def two_sum_sorted(nums, target):
    left, right = 0, len(nums) - 1
    while left < right:
        s = nums[left] + nums[right]
        if s == target:
            return True
        elif s < target:
            left += 1
        else:
            right -= 1
    return False
Python

  • Time Complexity: O(n)
  • Space Complexity: O(1)

Same Direction (Sliding Window / Expanding & Contracting)

  • Both pointers move forward but at different speeds.
  • Often used to maintain a window of elements that satisfy certain conditions.

Example Problem: Longest substring without repeating characters.

def longest_unique_substring(s):
    seen = set()
    left = 0
    max_len = 0
    
    for right in range(len(s)):
        while s[right] in seen:
            seen.remove(s[left])
            left += 1
        seen.add(s[right])
        max_len = max(max_len, right - left + 1)
    
    return max_len
Python

  • Time Complexity: O(n)
  • Space Complexity: O(n)

Fast & Slow Pointers (Tortoise and Hare)

  • One pointer moves one step at a time, the other moves faster.
  • Useful in linked lists and cycle detection.

Example Problem: Detect if a linked list has a cycle.

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def has_cycle(head):
    slow, fast = head, head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
        if slow == fast:
            return True
    return False
Python

  • Time Complexity: O(n)
  • Space Complexity: O(1)

Floyd’s Cycle Detection

Common Problems Solved with Two Pointers

Array Problems

  • Two Sum in sorted array
  • Trapping Rain Water
  • Remove Duplicates from Sorted Array
  • Container With Most Water

String Problems

  • Longest Substring without Repeating Characters
  • Valid Palindrome (check ignoring non-alphanumeric)
  • Minimum Window Substring (sliding window variation)

Linked List Problems

  • Detect cycle in linked list
  • Find middle of a linked list
  • Merge two sorted linked lists

Advantages of Two Pointers

  • Optimized – Reduces time complexity from O(n²) to O(n).
  • Simple & Intuitive – Easy to implement once recognized.
  • Versatile – Works with arrays, strings, and linked lists.

Tips to Master the Technique

  • Look for sorted input → often a hint.
  • Think about reducing brute force → Can we move one pointer instead of looping twice?
  • Practice sliding window problems → They are extensions of two pointers.
  • Visualize movement → Draw array/string with arrows for clarity.

Practice

Leetcode 167: Two Sum II – Input Array Is Sorted

class Solution:
    def twoSum(self, numbers, target):
        left, right = 0, len(numbers) - 1

        while left < right:
            current_sum = numbers[left] + numbers[right]

            if current_sum == target:
                return [left + 1, right + 1]  # 1-indexed
            elif current_sum < target:
                left += 1
            else:
                right -= 1
Python

Leetcode 125: Valid Palindrome

class Solution:
    def isPalindrome(self, s: str) -> bool:
        left, right = 0, len(s) - 1

        while left < right:
            # skip non-alphanumeric
            while left < right and not s[left].isalnum():
                left += 1
            while left < right and not s[right].isalnum():
                right -= 1

            if s[left].lower() != s[right].lower():
                return False

            left += 1
            right -= 1

        return True
Python

Other Questions

Conclusion

The two pointer technique is a must-know for anyone preparing for coding interviews or competitive programming. From searching pairs in sorted arrays to detecting cycles in linked lists, it significantly simplifies complex problems.

Leave a Comment