Two Pointers
17 / 170Two PointersLesson 2 of 6About 40 min
On sorted input, the current value tells you which pointer to move. That is the entire technique.
Run the two-pointer search on the sorted list [2, 4, 7, 11, 15] looking for a pair summing to 18. For each step give the sum of the two ends and which pointer moves next. Write the move as left, right, or done.
| step | left value | right value | sum | move |
|---|---|---|---|---|
| 1 | 2 | 15 | ||
| 2 | 4 | 15 | ||
| 3 | 4 | 11 | ||
| 4 | 7 | 11 |
In the sorted two-pointer search, the sum at left and right is smaller than the target, so you advance left. What justifies never reconsidering the old left value?
Read the constraints first and let them tell you what complexity is expected. Derive the approach, implement it, run the tests, and submit when it passes.