Practice Discrete Math

Algorithms / Two Pointers Compaction

Least You Need to Know: Read/Write Pointers, Compaction, and Deduping

A read pointer examines every element while a write pointer marks where the next kept element belongs. This is the core interview pattern for in-place filtering, removing duplicates, and compacting arrays without extra output storage.

جو کم از کم جاننا ضروری ہے

اہم علامتیں

read pointer scanning the full input
write index of the next kept output slot
kept prefix prefix `[0, write)` already rewritten correctly

مختصر حل شدہ مثال

  • Scan the array with `read`.
  • When an element should be kept, copy it to `a[write]` and advance `write`.
  • Ignore rejected elements by advancing only `read`.
  • At the end, the valid answer is the prefix of length `write`.

عام غلطیاں

اس قسم کے سوال کو کیسے پہچانیں

Next recommended lesson

Continue through this topic with Least You Need to Know: Opposite-End Two Pointers and Sorted Pair Search.

Least You Need to Know: Opposite-End Two Pointers and Sorted Pair Search

Related lessons

Keep going with nearby lessons in the same topic.

More ways to explore

مشق شروع کریں