Sort the sides, fix the largest each round, and let the gap between two pointers count every valid pair at once.
Given an array of positive side lengths, count how many triplets can form a valid triangle. A triangle is valid when the sum of its two shorter sides is greater than the longest one — checked naively, that's three inequalities per triple and overall.
Sorting collapses that down to one check per triple: once a ≤ b ≤ c,
proving a + b > c is enough — the other two inequalities hold
automatically, since c is already the largest. The two-pointer trick
then goes further, turning that one check into a bulk count rather
than a single triplet at a time.
Why fixing the largest side works
Count a whole range in one step
- With the largest side
cfixed andleft ≤ rightscanning everything smaller, ifnums[left] + nums[right] > cthen every index betweenleftandright - 1also sums pastcwhen paired withright— sorted order guarantees it. - That's
right - leftvalid triplets counted in work, not one comparison per triplet. - After counting, shrink
right— it's been paired with every smaller value that could possibly work.
What the shortcut does not do
a + b > conly proves a triangle once the array is sorted andcis confirmed the largest — applied to an arbitrary triple it proves nothing.- It only produces a count, not which sides were used — like 3Sum, the original positions aren't part of the answer.
- It assumes positive lengths; zero or negative values break the geometry the inequality depends on, not just the arithmetic.
- "triangle" or "form a triangle" alongside a list of side lengths
- counting every valid triplet, not just finding one
- positive integers as input
- comparing a sum of two sides against the third
The pattern
- 1Sort the array ascending.
- 2
Fix the largest side at index
k, walking inward from the end. - 3
Two-pointer the range before
k: sum too big → addright - leftto the count and shrinkright. - 4Sum too small → grow
left. Repeat for the nextk.
Complexity
An O(n) two-pointer sweep runs once per fixed largest side, dominating the O(n log n) sort.
Auxiliary space only — excludes the sort's internal use.
Common pitfalls
Checking all three inequalities
Once the array is sorted and c is the fixed largest side,
checking a + c > b and b + c > a too is redundant — and if the
array isn't sorted first, checking only one inequality is wrong.
Counting one triplet at a time
Incrementing the count by one and moving right by one on every
hit throws away the bulk-count insight and degrades to .
Letting the sweep touch the fixed index
The inner two pointers must stay strictly inside [0, k - 1] —
reaching k itself compares the fixed side against a copy of
itself.
Not shrinking after a hit
Every value from left to right - 1 is already counted against
the current right — staying on the same right recounts the
same range on the next iteration.
Variants worth knowing
- 1
3Sum — the same fix-one-and-two-pointer-the-rest shape, but proving an exact equality instead of counting a bulk range.
- 2
Container With Most Water — the same "sum exceeds a bound, so a whole side can be discarded" logic, but tracking the single best answer instead of a running count.
- 3
3Sum Smaller — counts triples under a target sum using the identical bulk-range trick, just without the triangle-specific largest-side fix.

