09 / 19

What is the Two-Pointer technique? Give a use case.

Two-Pointer Technique

javascript
  1. 1

    A classic use case is Two Sum in a sorted array.

  2. 2

    The technique can reduce an O(n²) brute-force search to O(n).

  3. 3

    It is also useful for palindrome validation and array partitioning.

  4. 4

    Correct pointer movement depends on an invariant, such as sorted order.

Difficulty: 3/10

Follow-up Questions

  • How is Two-Pointer different from Sliding Window?
  • Can Two-Pointer solve Three Sum?