Loading...
Loading...
Loading...
A craft shop has sticks of different lengths. Three sticks can form a triangle if the sum of any two is greater than the third. Count the ways to choose three sticks (by position in the list) that can form a triangle.
Example
Input
2 2 3 4
Output
3
Valid: (2,2,3), (2,3,4) twice. (2,2,4) is not valid.
Try the problem first. Open one hint at a time.
Sort the sticks. For the longest stick of a triple, the other two only need to add up to more than it.
Fix the longest stick and use two pointers on the shorter ones; when a pair works, every value between the pointers works too.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor