Count of Triangles

Practice this coding question on Coding Gurukul.

Problem statement

A workshop has a rack of rods and wants to catalogue all three-rod selections that can form a triangular frame. Each stored rod is a separate item, even when several rods have the same length. Flat frames and zero-length sides are not useful to the workshop. Your task Count the triples of distinct rod indices that form a triangle with positive area. You may choose rods from any positions, and each unordered triple is counted once. Important details Three lengths form a triangle only when the sum of the two shorter lengths is strictly greater than the longest. Equality produces a straight line and is invalid. Duplicate lengths are separate choices. Zero-length rods cannot participate in a valid triangle. Worked example For lengths 3 3 4 6, the valid selections are the two 3-length rods with 4, and either 3-length rod with 4 and 6. The total is 3. Choosing 3,3,6 fails because 3 + 3 equals 6. With fewer than three rods, print zero.

Input format

Read n, then n nonnegative rod lengths.

Output format

Print the number of valid index triples.

Constraints

1 <= n <= 1000; 0 <= length[i] <= 1000.

Sample input

4
3 3 4 6

Sample output

3

Sign in to run or submit a solution in C++, Java or Python.

Loading Coding Gurukul…