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
anshuman16423: 2018-12-03 15:22:41

Poor support for Python TLE with correct logic

masterchef2209: 2018-10-25 16:51:51

AC in 1 go

sort +upper_bound

Last edit: 2018-10-25 16:52:38
salman3007: 2018-10-05 04:07:16

consider each pair and apply binary search for the third

eagleshadow: 2018-10-02 12:27:36

AC in one go
simple sorting and upperbound/(or you can use binary search as well)

Last edit: 2018-10-02 12:27:50
kumarmapanip1: 2018-08-07 14:28:09

do we need to input the number of test cases??

alankrit_2107: 2018-07-31 18:52:20

Time- 0.03s Did it in O(n^2) much better than Binary search.

the_ashutosh: 2018-07-03 12:48:47

simple use vector and lower bound

spojabhi: 2018-05-30 10:08:23

tezi nhi bhaskar :)

aayushbhaskar: 2018-05-25 11:28:28

My 50th AC :)

ameyanator: 2018-05-15 19:56:23

hahaha AC in one go. Same runtime as @kaushikvallela!

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