Skip to content
playdsa
Preferences

Make yourself comfortable.

Saved on this browser. Your device’s reduced-motion preference is always respected.

Theme
Advanced settings

The Twin Compass

Learn
Play
Prove
Lesson 1 of 4

Live algorithm trace

Two Sum on sorted input

Complete execution
1 of 4

Two plus 15 is too large.

1left = 0; right = length - 1
2total = numbers[left] + numbers[right]
3if total == target: return [left + 1, right + 1]
4if total < target: left += 1
5else: right -= 1
target = 9total = 17

This time the stones are already sorted. That order is a compass: place one sentinel at each edge and let the sum tell you which one must move.

Help shape PlayDSA

Something confusing, broken, or missing? Leave a quick note without leaving your lesson.

Please leave out passwords, payment details and other private information.

Page included: /

Sign in to save feedback here, or send it with your email app. Your draft stays here while you sign in.

Open email instead