← Visualizers
Opposite-Ends Two Pointers Generalized

Generalized kSum / visualized

2Sum, 3Sum, and 4Sum are the same idea at different depths: fix pointers one at a time, and once only two free slots remain, collapse to the familiar converging two-pointer scan. Pick any k and watch the recursion stack grow and shrink.

Execution

idle
Press Run to begin.
0 / 0
Speed

Java · running line

level 1 level 2 level 3 level 4 left right found
O(n^(k-1)) time · O(k) recursion depth  ·  k−2 nested fixed pointers, then one O(n) two-pointer pass.