Data structures›Two pointers and sliding window
Tutorial 6 of 13 · Data structures · Arrays and lists · BeginnerTwo pointers and sliding window
Keep two positions in an array, move them by a rule, and avoid checking every pair.
8 lessons, the last is a challenge · about 1 h 30 min · Python and Java
Before you startArrays and dynamic arrays: indexes, reading and writing cells, and why inserting in the middle is slow
Hash maps in practice: counting with a map (used in the last lesson, and named as an option in the lesson on both ends)
Watch first · about 8 minTwo pointers: half a million pairs, under a thousand checks. The narration uses an AI clone of the creator’s voice.What you’ll be able to do
- 1Say what a pointer is in this tutorial, move one along an array, and read the cell it is on.
- 2Count the pairs in n numbers, and say why trying them all is O(n²): double the numbers, about four times the work.
- 3Move two pointers in from both ends of a sorted array, and explain why each move cannot lose the answer.
- 4Follow a read pointer and a write pointer along an array, and say what is true of the cells before write.
- 5Slide a window of fixed or changing width, updating its sum from the cells that enter and leave instead of adding it all again.
- 6Use these methods on strings, and say what Python and Java charge for a slice and for one character.
Lessons
Next in this path
Next: 7 · Sorted arrays and binary search · 6 lessons · 1 h 06 min
Find a value in a sorted array by cutting the search in half, again and again.
Playground
Try the same model with your own values. No checks, nothing recorded.
Open the playground