COMPILED CANVAS
DSA IN JAVA

Two loops → one pass

5 two pointer patterns

Tap to start →

PATTERN 1

Opposite ends

noon

One pointer at each end, moving inwards. Palindromes, reversing.

1 of 4 · keep tapping →
PATTERN 2

Pair sum (sorted)

124711

Sum too small? Move left up. Too big? Move right down.

2 of 4 · keep tapping →
PATTERN 3

Slow and fast

int slow = 0; for (int fast = 1; fast < n; fast++) if (a[fast] != a[slow]) a[++slow] = a[fast];

Fast reads, slow writes. Removes duplicates in place.

3 of 4 · keep tapping →
WHY

O(n) time, O(1) space

✗ Nested loops: O(n²)
✓ Two pointers: O(n)

Each pointer moves at most n times. No extra array.

4 of 4 · keep tapping →
⚠ MOST PEOPLE MISS THIS

The mistake

Using pair-sum pointers on an unsorted array. It only works when it's sorted.

QUICK QUIZ

Can you get it?

Tap an answer. The full video explains why.

📸 SCREENSHOT THIS

Cheat sheet

  • Opposite ends: palindromes
  • Sorted pair sum
  • Slow/fast: in-place
  • O(n) time
  • O(1) extra space
COMPILED CANVAS

Want the full lesson?

▶ Watch the full videoPractice + free notes

Free DSA in Java course. Subscribe to Compiled Canvas on YouTube.

Watch the full video