{
  "slug": "two-sum-sorted",
  "title": "Two Sum (Sorted)",
  "category": "Searching",
  "difficulty": "easy",
  "complexity": {
    "time": "O(n)",
    "space": "O(1)"
  },
  "idea": "On a sorted array, start a pointer at each end. If the pair sums too high, move the right pointer in; too low, move the left pointer in. They converge on the answer, or cross with no pair.",
  "code": [
    "lo = 0; hi = n-1",
    "while lo < hi:",
    "    s = a[lo] + a[hi]",
    "    if s == target: return (lo, hi)",
    "    if s < target: lo++   else: hi--"
  ],
  "example": {
    "array": [
      1,
      3,
      4,
      6,
      8,
      11,
      15
    ],
    "target": 14
  },
  "invariant": "Each step is the real data structure at that step — the picture is a pure function of it.",
  "stepCount": 4,
  "steps": [
    {
      "i": 0,
      "op": "say",
      "caption": "Find two values that add up to 14.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "array": [
        1,
        3,
        4,
        6,
        8,
        11,
        15
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [],
      "regions": []
    },
    {
      "i": 1,
      "op": "compare",
      "caption": "1 + 15 = 16 > 14 — too big, move the right pointer down.",
      "note": null,
      "codeLine": 5,
      "stats": null,
      "array": [
        1,
        3,
        4,
        6,
        8,
        11,
        15
      ],
      "focus": [
        "compare",
        null,
        null,
        null,
        null,
        null,
        "compare"
      ],
      "markers": [
        {
          "name": "lo",
          "index": 0,
          "tone": "bound"
        },
        {
          "name": "hi",
          "index": 6,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 2,
      "op": "compare",
      "caption": "1 + 11 = 12 < 14 — too small, move the left pointer up.",
      "note": null,
      "codeLine": 5,
      "stats": null,
      "array": [
        1,
        3,
        4,
        6,
        8,
        11,
        15
      ],
      "focus": [
        "compare",
        null,
        null,
        null,
        null,
        "compare",
        null
      ],
      "markers": [
        {
          "name": "lo",
          "index": 0,
          "tone": "bound"
        },
        {
          "name": "hi",
          "index": 5,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 3,
      "op": "found",
      "caption": "3 + 11 = 14. Found the pair at indices 1 and 5.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        1,
        3,
        4,
        6,
        8,
        11,
        15
      ],
      "focus": [
        null,
        "target",
        null,
        null,
        null,
        "target",
        null
      ],
      "markers": [
        {
          "name": "lo",
          "index": 1,
          "tone": "bound"
        },
        {
          "name": "hi",
          "index": 5,
          "tone": "bound"
        }
      ],
      "regions": []
    }
  ]
}