← Visualizers
Advanced Two Pointers LeetCode 259

3Sum Smaller / visualized

Count triplets whose sum is less than target — not find them, just count. The trick: once nums[i]+nums[left]+nums[right] < target, every right value between left and right also works with this left, since the array is sorted. That's right - left triplets counted in one shot.

Execution

idle
Press Run to begin.
0 / 0
Speed

Java · running line

fixed i left right batch counted
O(n²) time · O(log n)–O(n) sort space  ·  counting a whole range at once is what keeps this from degrading into brute-force O(n³).