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, so overall.
Sorting collapses that down to one check per triple. Once a ≤ b ≤ c,
proving a + b > c is enough, since c is already the largest and the
other two inequalities hold automatically. The two-pointer trick goes
further, turning that one check into a bulk count instead of one
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, supposenums[left] + nums[right] > c. Then every index betweenleftandright - 1also sums pastcwhen paired withright, since 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
- 1
Checking all three inequalities
Once the array is sorted and
cis the fixed largest side, checkinga + c > bandb + c > atoo is redundant, and if the array isn't sorted first, checking only one inequality is wrong. - 2
Counting one triplet at a time
Incrementing the count by one and moving
rightby one on every hit throws away the bulk-count insight and degrades to . - 3
Letting the sweep touch the fixed index
The inner two pointers must stay strictly inside
[0, k - 1]: reachingkitself compares the fixed side against a copy of itself. - 4
Not shrinking after a hit
Every value from
lefttoright - 1is already counted against the currentright: staying on the samerightrecounts 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.

