Neural Mastery
← Back to Practice

Pair With Target Sum

Difficulty: Easy · Pattern: Two Pointers · Concept: General Coding (DSA) — Core Patterns to Drill

Given a list of integers already sorted in ascending order and a target, find the indices of the two numbers that add up to target. Assume exactly one valid pair exists, and that you can't use the same element twice.

The brute-force answer checks every pair — O(n2)O(n^2). Sorted input is the tell that a smarter approach exists: two pointers, one starting at each end, moving inward based on whether the current sum is too big or too small.

Your task: implement pair_with_target_sum(arr, target), returning [i, j] with i < j.

Implement it yourself
assert pair_with_target_sum([1, 2, 3, 4, 6], 6) == [1, 3] assert pair_with_target_sum([2, 5, 9, 11], 11) == [0, 2] assert pair_with_target_sum([1, 3, 5, 7, 9, 11], 16) == [2, 5] assert pair_with_target_sum([-3, 0, 1, 2, 5], 2) == [0, 4]

Next: Three Sum to Zero (a harder variant), or continue to Longest Run of Unique Characters

Last updated Sep 5, 2026Edit this pageReport an issue