Two Sum II - Input Array Is Sorted
mediumTests passed 2026-11-11v1.0.01. Core Invariant / The Trick
Sorted order guarantees monotonic sum adjustments: if `nums[l] + nums[r] > target`, only decreasing `r` can lower the sum; if `< target`, increment `l`. Return 1-based indices.
2. Complexity Bounds & Proof
Eliminates one row or column of the candidate matrix per step.
3. Minimal Idiomatic Code
def twoSumSorted(numbers: list[int], target: int) -> list[int]:
l, r = 0, len(numbers) - 1
while l < r:
s = numbers[l] + numbers[r]
if s == target: return [l + 1, r + 1]
if s < target: l += 1
else: r -= 1
return []4. Failure Modes & Interview Follow-ups
Pitfalls:- Problem requires 1-based indexing `[l + 1, r + 1]`.
Follow-ups:- 3Sum freezes one pointer and calls this 2-pointer reduction.
Personal Notes & Scratchpad (Local-First Yjs CRDT)
Saved instantaneously in IndexedDB. Streams live across peers via Hocuspocus when signed in.
Related Invariants
Browser Scratchpad
Console Output
Click Run or press Cmd/Ctrl+Enter to execute locally in a Web Worker.