{
    "slug": "two_pointer_technique",
    "term": "Two-Pointer Technique",
    "category": "algorithms",
    "difficulty": "intermediate",
    "short": "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²).",
    "long": "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.",
    "aliases": [
        "two-pointer approach",
        "Floyd's algorithm"
    ],
    "tags": [
        "algorithms",
        "arrays",
        "optimisation"
    ],
    "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."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "big_o_notation",
        "sliding_window",
        "sorting_algorithms"
    ],
    "prerequisites": [
        "big_o_notation",
        "sliding_window",
        "sorting_algorithms"
    ],
    "refs": [
        "https://en.wikipedia.org/wiki/Two-pointer_technique"
    ],
    "bad_code": "// O(n²) — nested loop to find pair summing to target:\nfunction hasPairWithSum(array $arr, int $target): bool {\n    for ($i = 0; $i < count($arr); $i++)\n        for ($j = $i + 1; $j < count($arr); $j++)\n            if ($arr[$i] + $arr[$j] === $target) return true;\n    return false;\n}",
    "good_code": "// O(n log n) sort + O(n) two-pointer = O(n log n) total:\nfunction hasPairWithSum(array $arr, int $target): bool {\n    sort($arr);\n    $left = 0; $right = count($arr) - 1;\n    while ($left < $right) {\n        $sum = $arr[$left] + $arr[$right];\n        if ($sum === $target) return true;\n        $sum < $target ? $left++ : $right--;\n    }\n    return false;\n}",
    "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",
    "severity": "medium",
    "effort": "medium",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/two_pointer_technique",
        "html_url": "https://codeclaritylab.com/glossary/two_pointer_technique",
        "json_url": "https://codeclaritylab.com/glossary/two_pointer_technique.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Two-Pointer Technique](https://codeclaritylab.com/glossary/two_pointer_technique) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/two_pointer_technique"
            }
        }
    }
}