DSA · Lesson 2 of 9
Arrays & two pointers
Solving array problems in one pass with two moving indexes.
The two-pointer technique keeps two indexes that move towards each other or in the same direction, avoiding a nested loop.
- Opposite ends — sorted pair sum, palindrome check, reversing in place.
- Slow/fast — removing duplicates from a sorted array, moving zeros to the end.
A close cousin is the sliding window: keep a window [left, right] and update a running sum or count as it slides, e.g. “maximum sum of k consecutive days”.
Try it yourself
Press Run to see the output.
Edit and press Run (or Ctrl/⌘ + Enter). Python runs in your browser — the first run downloads the Python engine and takes a few seconds.
Key points
- Two pointers turn many O(n²) pair searches into O(n).
- Opposite-end pointers need sorted data.
- Sliding window = add the new item, remove the old one.
Quick check
Why must the array be sorted for the opposite-ends pair-sum method?
Sorting guarantees moving left increases the sum and moving right decreases it.
Practice task
Write move_zeros(nums) that moves all 0s to the end in place while keeping the order of other numbers.
Interview corner
How would you find two numbers summing to a target in an unsorted array?
Use a dict of value → index: for each x, check if target − x is already in it. O(n) time, O(n) space.
What is the sliding window pattern?
Maintaining a contiguous range and updating its state incrementally as it expands/shrinks, instead of recomputing from scratch.
Further reading
Official documentation, linked for deeper study. These are outside sites; this lesson's text is our own.
Original lesson written by the InfoparkDaily team · © 2026 InfoparkDaily · Free to learn — no paid course or subscription.