Skip to content

Pair Sum in a Sorted Array
Step by Step

Two pointers start at both ends. Too big a sum moves r left, too small a sum moves l right, and every move rules one number out for good.

Step through every pointer move in both directions, then share a link to the step where the pair showed up.

What You Watch While Both Pointers Close In

The two indexes, the sum and the ruled out span change on every step.

Sorting is the clue. Sums above the target only shrink when r moves left, and sums below it only grow when l moves right.

CodeStory's visualiser shows both indexes walking and the span they ruled out. Marked code lines follow every move, and you can step back, step forward or play.

Open Partner skipped first. Then load No pair to see every step still rule one number out.

How Pair Sum Closes In on the Target

Steps below follow the two indexes on your screen.

  1. 1Place l at the first number and r at the last.
  2. 2Add the two numbers and compare the sum with the target.
  3. 3Move r left when the sum is too big, or move l right when it is too small.
  4. 4Stop on a match, or when l and r meet.

Pair Sum Mistakes the Visualiser Shows

A switch in the visualiser turns the mistake on, so you can watch the state go wrong.

  • Brute force: every pairIgnores the sorting and tries every pair, about n²/2 checks instead of n − 1.
  • Bug: move both pointersAfter a miss it moves l and r together, dropping a number nobody proved useless. Load Partner skipped to see it.

Pair Sum Inputs Worth Trying

Three presets cover a skipped partner, a missing pair and unsorted input.

  • Partner skippedSwitch on the move-both bug here. It drops 1 while its partner 6 is still inside.
  • No pairEven numbers with an odd target give no pair, yet every step still rules one number out.
  • Unsorted inputInput is sorted first, so the indexes are positions in the sorted array.

Pair Sum Cost in Time and Space

Every move rules one number out, so the pointers meet after about n steps.

Time
O(n)counted from the moves you step through
Space
O(1)two index values besides the input
Pattern
Two pointersTwo pointers rule out one number per move.

Keep Going With Two Pointers Practice

Watching shows the pointers and solving trains you to choose which one moves.

Everything in the App in One Place

Practice, interviews and tools all start free. Swipe to see the rest.

Sign up for 100 AI credits now and get 100 more every month. Start free in the app

Pair Sum Visualiser FAQs

Open the visualiser and follow both pointers inward.

Open the visualiser
What does the pair sum in a sorted array visualiser show?

CodeStory's visualiser shows both indexes walking inward and the span they ruled out, beside the highlighted code. Move back and forth through every step, or let it play.

What is the time complexity of pair sum in a sorted array?

O(n) time and O(1) space. Live counts show about n steps instead of every pair.

Why does my pair sum code fail on some inputs?

Moving both pointers after a miss is a common cause. Switch the bug on in the visualiser and watch a number leave while its partner is still inside.

Can I use my own input?

Yes. Enter your own input and the visualiser plays the algorithm on it, including the edge cases that break a solution in an interview.

Is the visualiser free?

Yes. Every new account starts with 100 AI credits and gets 100 more monthly, while watching an algorithm costs nothing.

Stuck on Two Pointers? Get a Free Callback

Tell CodeStory which two pointers step loses you, and a mentor plans what comes next.

  1. 1CodeStory checks your level, from two pointers basics to interview ready.
  2. 2CodeStory explains online and campus batches, with timings for your weekdays or weekends.
  3. 3You decide. No pressure, no payment on the call.

Get a free callback

I am a
I want to learn

Your number is only used for a CodeStory callback.