← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Two-Pointer Technique

algorithms Intermediate

Also Known As

two-pointer approach Floyd's algorithm

TL;DR

An algorithmic pattern using two indices that move through a sorted array to find pairs or subarrays satisfying a condition in O(n) instead of O(n²).

Explanation

Two pointers typically start at opposite ends of a sorted array (or one at start, one ahead). By moving them based on comparison results, problems that would require nested loops become linear. Classic uses: finding a pair that sums to a target, removing duplicates from a sorted array, merging two sorted arrays. The fast/slow pointer variant (Floyd's algorithm) detects cycles in linked lists.

Diagram

flowchart TD
    ARR[Sorted array: 1 2 4 6 8 10<br/>Find pair summing to 10]
    subgraph TwoPointer
        L[Left pointer at 1]
        R[Right pointer at 10]
        CHECK{Sum = 11 > 10}
        CHECK -->|too big - move right left| R2[Right moves to 8<br/>1+8 = 9 < 10]
        R2 -->|too small - move left right| L2[Left moves to 2<br/>2+8 = 10 found!]
    end
    subgraph Complexity
        NAIVE[Naive O of n squared<br/>check all pairs]
        TP[Two pointer O of n<br/>one pass]
    end
style L fill:#1f6feb,color:#fff
style R fill:#d29922,color:#fff
style L2 fill:#238636,color:#fff
style NAIVE fill:#f85149,color:#fff
style TP fill:#238636,color:#fff

Common Misconception

Two pointers only work on sorted arrays — fast/slow pointers (cycle detection) and the sliding window variant work on unsorted data too.

Why It Matters

Turns O(n²) pair-finding problems into O(n), a common interview technique with real applications in database join algorithms and merge operations.

Common Mistakes

  • Not sorting the array first when the algorithm requires sorted input.
  • Moving both pointers by one each iteration — the correct move depends on which pointer's value is larger.
  • Off-by-one errors in the loop termination condition — use strict less-than for opposite-end pointers.
  • Forgetting that this technique requires sorted data for sum/target problems.

Code Examples

✗ Vulnerable
// O(n²) — nested loop to find pair summing to target:
function hasPairWithSum(array $arr, int $target): bool {
    for ($i = 0; $i < count($arr); $i++)
        for ($j = $i + 1; $j < count($arr); $j++)
            if ($arr[$i] + $arr[$j] === $target) return true;
    return false;
}
✓ Fixed
// O(n log n) sort + O(n) two-pointer = O(n log n) total:
function hasPairWithSum(array $arr, int $target): bool {
    sort($arr);
    $left = 0; $right = count($arr) - 1;
    while ($left < $right) {
        $sum = $arr[$left] + $arr[$right];
        if ($sum === $target) return true;
        $sum < $target ? $left++ : $right--;
    }
    return false;
}

Added 15 Mar 2026
Edited 22 Mar 2026
Views 26
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 1 ping S 2 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 2 pings S 0 pings S 0 pings M 0 pings T 1 ping W 0 pings T 1 ping F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Amazonbot 7 Perplexity 6 Unknown AI 3 Google 3 Ahrefs 2
crawler 19 crawler_json 1 pre-tracking 1
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Medium
⚡ Quick Fix
Use two pointers (start/end) on sorted arrays for problems requiring pairs — merge sorted arrays, find pairs summing to target, reverse in place; O(n) vs O(n²) brute force
📦 Applies To
any web cli
🔗 Prerequisites
🔍 Detection Hints
Nested loop on sorted array finding pairs; O(n²) solution for problem on sorted data where two pointers gives O(n)
Auto-detectable: ✗ No
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: Medium Context: Function Tests: Update

✓ schema.org compliant