NOTATRI - Not a Triangle

You have N (3 ≤ N ≤ 2,000) wooden sticks, which are labeled from 1 to N. The i-th stick has a length of Li (1 ≤ Li ≤ 1,000,000). Your friend has challenged you to a simple game: you will pick three sticks at random, and if your friend can form a triangle with them (degenerate triangles included), he wins; otherwise, you win. You are not sure if your friend is trying to trick you, so you would like to determine your chances of winning by computing the number of ways you could choose three sticks (regardless of order) such that it is impossible to form a triangle with them.


The input file consists of multiple test cases. Each test case starts with the single integer N, followed by a line with the integers L1, ..., LN. The input is terminated with N = 0, which should not be processed.


For each test case, output a single line containing the number of triples.


4 2 10
1 2 3
5 2 9 6


For the first test case, 4 + 2 < 10, so you will win with the one available triple. For the second case, 1 + 2 is equal to 3; since degenerate triangles are allowed, the answer is 0.

hide comments
mourad_tu: 2018-03-22 13:12:21


raghav_chawla7: 2018-02-26 10:29:05

sticks of equal length are considered different

kaushikvallela: 2018-02-04 08:04:44

AC in one go...
simple sorting and binary search...
O(n^2*logn) Time-0.18s

codeking123: 2018-01-25 16:12:52

good for beginers

avik26091998: 2017-12-02 14:33:13

two pointers..

stranger77: 2017-08-20 09:00:00

sticks of equal length is considered different because of labelling from 1 to N

suchith: 2017-08-05 08:23:39

done with O(n^2*log(n)). good problems for beginners and there can be multiple sticks with same length

codiyapa420: 2017-05-23 14:27:45

@lalwr nice fake alca :)

deep_1: 2017-03-23 04:06:38

easy O(n^2*lg(n)) ..... AC in one go!!!!!

nilabja16180: 2017-03-22 12:43:14

There can be multiple stick of same length, got me a WA!

Added by:Neal Wu
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO