Preploop
Patterns
DSA Patterns
Two PointersSDE I
Two indices scanning toward each other or at different speeds
O(n) O(1)
0/182
Problem

Given a sorted array, find two numbers that add up to the target and return their indices.

Visualizer step generation error. Reset inputs to run.
Controls
How to recognize this pattern
  • Problem gives a SORTED array and asks for a pair/triplet summing to X
  • Brute force is O(n²) nested loops — you need O(n)
  • Keywords: "two sum", "three sum", "palindrome", "remove duplicates in-place"
  • You can shrink the problem from both ends simultaneously
When to use this pattern

Instead of nested loops O(n²), use two indices that together cover the search space in a single pass. The sorted order lets you decide which pointer to move.

  • Sorted array and you need pairs/triplets that meet a condition
  • Partitioning: move all negatives left, positives right
  • Palindrome check, removing duplicates in-place
  • Fast + slow pointer: cycle detection, middle of linked list
1 / 1
Speed
Input