LeetCodechevron_rightSolve patternschevron_righttwo pointer invariant
schemaReusable solving pattern

two pointer invariant Pattern

Pattern hubs are for building transferable solving frames. Learn the recognition signals first, then drill state definition, update rules, and edge explanation until the pattern feels stable.

database99 problemstune37/56/6 difficulty mixcategory6 linked topics

Pattern brief

Recognize first

Do you understand the two-pointer technique and how it helps reduce the time complexity of this problem?

Solve rhythm

State the active state and invariant first, explain how each update preserves them, then pressure-test with counterexamples.

Most common miss

Failing to optimize the solution and instead using a brute-force approach that simulates all pairs, leading to O(n^2) time complexity.

Recognition signals

  • Do you understand the two-pointer technique and how it helps reduce the time complexity of this problem?
  • Can you explain why we always move the pointer pointing to the shorter line in the two-pointer approach?
  • Do you see how sorting simplifies duplicate management in two-pointer scanning?

Solve flow

  1. 1. Define the active state/window.
  2. 2. Update state while preserving invariants.
  3. 3. Validate with edge-heavy examples.

Common misses

  • Failing to optimize the solution and instead using a brute-force approach that simulates all pairs, leading to O(n^2) time complexity.
  • Failing to skip duplicate fixed elements leads to repeated triplets in output.
  • Forgetting to sort the array first, which can lead to inefficient solutions or missed results.

Recommended Ladder

Problem bank

two pointer invariant pattern bank

Start by scanning with search or difficulty filters, then narrow by linked topics. The bank continues loading inside its own container so the page stays readable.

Progressive pattern bank

Use it to build pattern understanding first, then expand into the full corpus.

hourglass_bottomScroll inside to continue
search
tuneDifficulty
categoryTopic focus

Showing 24 / 99 problems

+24 per load
#TitleDifficulty
11

Container With Most Water

Find two vertical lines that can form a container with the most water in a given array of heights.

Medium
15

3Sum

Given an integer array, return all unique triplets where the sum is zero using two-pointer scanning with careful duplica…

Medium
16

3Sum Closest

Find the sum of three integers in an array that is closest to a given target using two-pointer scanning.

Medium
18

4Sum

The 4Sum problem requires finding all unique quadruplets in an array that sum to a specific target value.

Medium
26

Remove Duplicates from Sorted Array

Learn how to remove duplicates from a sorted array in-place using two-pointer scanning while preserving element order.

Easy
27

Remove Element

Remove Element challenges you to remove a value from an array in-place using efficient two-pointer scanning and tracking…

Easy
28

Find the Index of the First Occurrence in a String

Locate the first occurrence of a substring within a string using a two-pointer scanning strategy and invariant tracking …

Easy
31

Next Permutation

Next Permutation is a medium-difficulty problem focusing on generating the next lexicographically greater permutation of…

Medium
75

Sort Colors

Sort Colors requires in-place reordering of an array using a two-pointer scanning strategy to group 0s, 1s, and 2s effic…

Medium
80

Remove Duplicates from Sorted Array II

Solve the problem of removing duplicates from a sorted array in-place, ensuring each element appears at most twice, usin…

Medium
88

Merge Sorted Array

Merge two sorted arrays into the first array in non-decreasing order, using a two-pointer approach.

Easy
125

Valid Palindrome

Check if a given string is a valid palindrome by using two-pointer scanning and invariant tracking techniques.

Easy
151

Reverse Words in a String

Reverse Words in a String requires reordering words using precise two-pointer scanning with careful space handling for e…

Medium
165

Compare Version Numbers

Compare two version numbers by checking their individual revisions, considering missing revisions as zero, using a two-p…

Medium
189

Rotate Array

Rotate Array challenges you to shift elements right by k steps using precise two-pointer scanning and invariant tracking…

Medium
202

Happy Number

Determine if a number is happy by repeatedly summing squares of digits using two-pointer scanning to detect cycles effic…

Easy
283

Move Zeroes

Move all zeros in an array to the end while preserving the order of non-zero elements using efficient in-place operation…

Easy
295

Find Median from Data Stream

Implement a MedianFinder class that supports adding numbers and finding the median from a data stream.

Hard
321

Create Maximum Number

Create Maximum Number involves merging digits from two arrays while preserving order, maximizing the resulting number.

Hard
344

Reverse String

Reverse a character array in-place using a two-pointer scanning technique, ensuring minimal memory and correct index swa…

Easy
345

Reverse Vowels of a String

Reverse Vowels of a String uses a two-pointer approach to reverse the vowels in a string while leaving the consonants in…

Easy
443

String Compression

Compress a character array in-place by converting consecutive repeated characters into counts using two-pointer scanning…

Medium
455

Assign Cookies

Maximize content children by assigning at most one cookie per child using two-pointer scanning and greedy sorting techni…

Easy
481

Magical String

Count the number of '1's in the first n characters of a magical string using two-pointer scanning and invariant tracking…

Medium

swap_vertScroll inside the bank to auto-load more

Continue by topic

Once the pattern itself feels familiar, move back into concrete topic hubs so you can separate the pattern from the changing problem context.

route

Guided Practice Path

AI recommends problems by your level and tracks your progress.

Start Guided Patharrow_forward
Two-pointer scanning with invariant tracking LeetCode Pattern: 99 Solutions