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
da_201501181: 2017-02-12 15:10:41

AC in one GO..!!
O(n^2*logn) Time-0.29s

Last edit: 2017-02-12 15:18:59
sarthakshah30: 2016-12-30 14:32:24

[code snipped]

Last edit: 2016-12-30 16:26:48
smtcoder: 2016-09-03 14:25:48

my 50th.... :)
AC in 1 go with O(n^2)..

Anuj Arora: 2016-08-21 12:49:31

Phew........lot of corner cases....nice binary search problem..........better to get a O(n^2) soln

iharsh234: 2016-08-16 21:30:08

O(n^2) best timed solution.

lalywr: 2016-07-25 23:24:24

O(n*log(n)) I Rule :D

vineetpratik: 2016-07-05 19:38:44

binary search 0.16 , sliding pointers/traversing array in optimized way 0.03 seconds

mkfeuhrer: 2016-07-02 22:38:08

nyc n easy 200 AC :-) think obvious with binary search!!

Dilpreet: 2016-06-28 05:58:43

Implemented O(n^2*log(n)) in java got TLE.
Same implementation gets AC in C++.

Last edit: 2016-06-28 05:58:54
nonushikhar: 2016-06-16 19:15:56

ac in one go O(n^2*log(n))
with little optimisation

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