
3Sum
A reliable 3Sum solution comes from turning a cubic search into a sequence of sorted two-sum scans—and proving why each pointer move is safe.
View solutionReasoning that prevents, removes, detects, or safely tolerates repeated values or repeated candidate results as a central correctness obligation.
Tagged articles
21 articles in this tag.

A reliable 3Sum solution comes from turning a cubic search into a sequence of sorted two-sum scans—and proving why each pointer move is safe.
View solution
Four choices suggest an O(n^4) search. Sorting changes the last two choices into a controlled walk.
View solution
The hard part is not finding combinations that add to the target. It is finding them once while respecting the physical number of occurrences in the input.
View solution
Treat this as an enumeration problem, not a permutation problem. Sort the candidates, keep combinations in nondecreasing order, recurse from the same index…
View solution
The duplicate-ordering trap is the whole problem: [1, 2] and [2, 1] represent one selection, not two. Build every path in increasing order, and the…
View solution
Repeated characters create the trap: different selections from s can produce the same visible text in t, and the problem still counts those selections…
View solution
A standard binary search finds a match. This problem asks for the entire matching block. The difference is one boundary decision.
View solution
The array is not just input. Under the right invariant, it becomes its own presence map.
View solution
A repeated character is not a reason to restart the scan. It is a reason to move the left boundary to the first position that makes the current window…
View solution
Merging is the obvious solution. It is also disqualified by the runtime requirement. The useful reframe is to search for a cut, not for a value: place…
View solution
The hard part is not moving two pointers. It is preserving the target’s multiplicity while the window changes.
View solution
The reliable way to solve Next Permutation is to stop thinking in four memorized steps. Read the suffix, identify where it is already maximal, then make…
View solution
When nums = [1, 1, 2], ordinary permutation backtracking treats the two 1 values as different input positions. That creates duplicate value sequences.
View solution
The trap is treating duplicate removal as a counting problem. The sharper model is an input stream and a compacted result prefix: read every candidate,…
View solution
The word “remove” is misleading here. You do not need to shrink the Python list or delete values from its tail. You need to compact the distinct values…
View solution
The key distinction is easy to miss: this problem removes every node belonging to a repeated value. It does not keep the first occurrence. The solution is…
View solution
When a problem says “remove duplicates,” the first instinct is often to reach for a set. That works for an unsorted list, but it misses the key clue here:…
View solution
Duplicates turn a clean binary-search decision into an information problem. When nums[left], nums[mid], and nums[right] are equal, you cannot tell which…
View solution
The duplicate bug comes from treating equal input positions as different decisions. Sort first, then skip equal candidates only when they are siblings at…
View solution
Equal-width tokens turn a permutation problem into a finite set of aligned frequency windows.
View solution
A BST validator must remember the ancestors that still constrain the current node.
View solution