← Visualizers
Opposite-Ends Two Pointers LeetCode 611

Valid Triangle Number / visualized

Count triplets that can form a triangle's sides. Sort first, then fix the largest side k from the end inward. For a fixed largest side, if nums[left] + nums[right] > nums[k], then every value between them also works with right — count all of them at once and move right in.

Execution

idle
Press Run to begin.
0 / 0
Speed

Java · running line

fixed largest side (k) left right counted range
O(n²) time · O(log n) sort space  ·  the triangle inequality only needs checking on the two shortest sides — the third is automatic once sorted.