Skip to solution
mediumDSA

What is the two-pointer technique?

193 views
01

Understand the problem

Explain two pointers.

two-pointers
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Two pointers uses two indices moving through data (often sorted) to reduce nested loops to O(n) — e.g. finding a pair summing to a target, reversing in place, or removing duplicates. A sliding-window variant maintains a range for substring/subarray problems.

Solution ready — 2 min read

Classified // press E to declassify

04

Read the code

Pair sum in a sorted array
Run Playground
def pair_sum(arr, target):
    left, right = 0, len(arr) - 1
    while left < right:
        s = arr[left] + arr[right]
        if s == target:
            return (left, right)
        if s < target:
            left += 1
        else:
            right -= 1
    return None


# --- demo ---
print(pair_sum([2, 7, 8, 11, 15, 18], 18))   # (1, 3)  -> 7 + 11
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 91 of 127 decoded in the Data Structures & Algorithms track. One more won't hurt.

Back to track