CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Practice(easy) » Not a Triangle

Not a Triangle

Problem code: NOTATRI

  • Submit
  • All Submissions

All submissions for this problem are available.

A tutorial for this problem is available here.

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.

Input

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.

Output

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

Example

Input:
3
4 2 10
3
1 2 3
4
5 2 9 6
0

Output:
1
0
2

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.


Author: admin
Date Added: 1-12-2008
Time Limit: 2 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TCL, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

tarinbansal @ 12 Jul 2009 06:15 AM

What does M stand for in memory = 2.5M? Is it MB or what?

chang @ 12 Jul 2009 08:40 PM

can 2 sticks have same length ?

chang @ 12 Jul 2009 09:41 PM

got it :D they can be same.

admin 2 @ 13 Jul 2009 06:44 PM

@tarin Yes, it stands for MB

gauravsharma @ 29 Jul 2009 08:01 PM

Time 2s s== ?

triplem @ 29 Jul 2009 08:02 PM

Seconds..

atulbansal128 @ 2 Aug 2009 06:12 AM

can some sticks be of equal length. if yes then should we consider triplets like 1 2 4 and 1 2 4 different or same?

admin 2 @ 2 Aug 2009 08:26 AM

@atul It seems you've figured that out yourself :)

atulbansal128 @ 2 Aug 2009 06:12 PM

i concluded answer is yes to my question

I am considering 2 triplet of

udayabhanu @ 19 Aug 2009 10:12 PM
I am considering 2 triplet of same members to be different, since, you are saying "at random" and "probability"?! Is that fine to assume so?

Yes, if I understand you

admin @ 20 Aug 2009 01:33 AM

Yes, if I understand you correctly, you are right in assuming so. :)

i want information about how

gadige.siddharth @ 15 Sep 2009 02:27 PM

i want information about how to improve my skills and al and i m MBA student so guide me plz

if degenrate triangles are

gunjanbansal @ 10 Dec 2009 11:09 PM

if degenrate triangles are allowed , then y is ans of second test case 0 ??

shouldnt it be 1 ??

i got it , my mistake ,

gunjanbansal @ 10 Dec 2009 11:26 PM

i got it , my mistake ,

i dnt seem to find y i am

gunjanbansal @ 12 Dec 2009 08:47 PM

i dnt seem to find y i am getting wrong ans.. plz help

what is the problem wid

gunjanbansal @ 12 Dec 2009 09:57 PM

what is the problem wid foloowing search algo ..

 

count=0;
for(i=0;i<sortindex-2;i++)//0 1  2 5 6 9
{
for(tmp=i+1;tmp<sortindex-1;tmp++)//1 2
{
n=sorted[i]+sorted[tmp];
lb=tmp+1;

ub=sortindex-1;
mid=(lb+ub)/2;
while(lb<ub)
{
if(sorted[mid]>n)
{
ub=mid-1;
mid=(lb+ub)/2;
}
else
if(sorted[mid]<n)
{
lb=mid+1;
mid=(lb+ub)/2;
}
else
break;
}
while(sorted[mid]==n)
mid++;
if(sorted[mid]>n)
count+=sortindex-mid;

}
}

plz help I wrote the

shaurya @ 25 Jan 2010 12:35 AM

plz help

I wrote the following code:

#include<stdio.h>
void sort_merge_aux(int , int);
void sort_merge(int, int, int);
//int p=0;
long int arr[2000];
long int temp[1000];

void sort_merge_aux(int low,int high)
{
int mid;
if(low!=high)
{
mid=(low+high)/2;
sort_merge_aux(low,mid);
sort_merge_aux(mid+1,high);
sort_merge(low,mid,high);
}
}
void sort_merge(int low,int mid,int high)
{
int i,j,k;
i=low;
j=mid+1;
k=low;
while((i<=mid)&&(j<=high))
{
if(arr[i]>=arr[j])
temp[k++]=arr[j++];
else
temp[k++]=arr[i++];
}
while(i<=mid)
temp[k++]=arr[i++];
while(j<=high)
temp[k++]=arr[j++];
for(i=low;i<=high;i++)
arr[i]=temp[i];
}
int search_number(long int k,int lower,int num)
{     
int mig,low,mid,high,found=0,var;
//var=-1;
// mig=0;
low=lower;
high=num-1;

while(low<=high)
{
mid=(low+high)/2;
if(k>arr[mid])
low=mid+1;
else if(k<arr[mid])
high=mid-1;
else
{
//    found=1;
//    var=mid;
break;
}
}
if(high<lower)
return (high+1);   
if(low>num-1)
return low;
if(low<=high)
{
while(arr[mid]==k)
mid++;       
return mid;
}
else
{
if(arr[high]>k&&arr[low]>k)
return high-1;
else if(arr[high]<k&&arr[low]>k)
return (high+1);
//else if(arr[high]>k&&arr[low]<k)
//        return  ;
else if(arr[high]<k&&arr[low]<k)
return low+1;            
}                  
}

int main()
{
long nt,n,m,p,not_a_triangle;
int t,num;
register long int i,j,k;
scanf("%d",&num);
while(num!=0)
{
t=0;
while(t<num)
{
scanf("%d",&arr[t]);
t++;
}
sort_merge_aux(0,num-1);
not_a_triangle=0;
for(i=0;i<num-2;i++)
{
for(j=i+1;j<num-1;j++)
{
not_a_triangle+=num-search_number(arr[i]+arr[j],j+1,num);
}
}
printf("%dn",not_a_triangle);
scanf("%d",&num);
}
//getch();
return 0;
}

but I get runtime error.i have checked for

-memory limit is not exceeded if long int is of 8byte or less

-array bounds are also not exceeded

please, fellow coders, do NOT

jcomeau_ictx @ 25 Jan 2010 11:42 AM

please, fellow coders, do NOT post code here. besides it being hard to read, some people would rather solve the problems themselves without getting hints. see the FAQ for how to post a link to your submission, and people who want to can then see it, properly formatted, there.

plz help! ok! i read faqs but

shaurya @ 25 Jan 2010 07:21 PM

plz help!

ok! i read faqs but from where can i get

link to the solution i submit

from the problem page, say:

jcomeau_ictx @ 25 Jan 2010 09:39 PM

from the problem page, say: http://www.codechef.com/problems/TEST/

to the right of the headline "Life, the Universe, and Everything" you will see 3 buttons: "ALL SUBMISSIONS", "MY SUBMISSIONS", and "SUBMIT". Click "MY SUBMISSIONS".

You will see a list of your submitted programs. Pick the one you want, and on the far right of the row, click "View Solution".

Copy and paste the link from your browser into the Comment page: http://www.codechef.com/viewsolution/110651

I'll review the FAQ, and add this info if needed.

Anyway, I'm not clear on why anyone should help debug other people's, i.e. YOUR, programs. You don't learn nearly as much if someone does it for you. During a contest, nobody is allowed to assist you anyway, and if you have been getting help all along, you'll be, quite literally, helpless. Especially since it seems many contest problems are described incorrectly or the sample input/output is wrong. Do you remember the battleship problem, I think it was in November? The testcases were wrong, but someone solved it anyway before the admins fixed it. Now THAT was a real coder... he not only debugs his programs, he debugged the problem description itself, without assistance.

In first test case, in above

Tanuja @ 9 Mar 2010 03:33 PM

In first test case, in above Ex, how we get one triple? 4+2<10

actually length of largest side should be lesser than sum of other two sides.

Read the problem statement.

triplem @ 9 Mar 2010 04:07 PM

Read the problem statement. You are not asked to print out the number of valid triples.

Can anyone name  a few

kuarc @ 27 Aug 2010 08:58 AM

Can anyone name  a few boundary test cases where my prog could be failing :(   i have tried a lot of em.. but passes here.. The overall result in judge is Wrong Answer

hiii

sumit_lnmiit @ 1 Sep 2010 12:12 PM

hiii

how to see the input output

keybo1312 @ 27 Sep 2010 11:27 PM

how to see the input output set used...

I have solved the problem as

shubh09 @ 23 Dec 2010 11:17 PM

I have solved the problem as explained in the tutorial (I have used merge sort, by the way), but I am still geting TLE. Moreover, my code is running perfectly within the time limit of 2s on my computer (It gave the answer in 0.17 seconds for N=2000 and N random positive numbers less than 1000000 as input for the lengths of the sticks). I even compared the execution time with that of an accepted solution with the same input, and the accepted solution gave the answer in 0.18 seconds.

Here is the link to my solution: http://www.codechef.com/viewsolution/406062.

Hi, How to figure out number

sachinpkale @ 15 Mar 2011 04:50 PM

Hi,

How to figure out number of test cases in advance?

or What is the max no. of test cases?

It is required as I am using C and I have to create array to store results.

It is giving me Runtime Error and I think it is because I was using array of 1000 to store the results.

#include<iostream>#include<co

jatingulati25 @ 6 May 2011 04:56 PM

#include<iostream>
#include<conio.h>
using namespace std;
int main(){
int i,j,k,n,count=0,a[10];
int sum2,sum3;
int max;
while(1){
cin>>n;
if(n==0)
return 0;
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n-2;i++){
max=a[i];

for(j=i+1,sum2=0;j<n-1;j++){
if(a[j]>max){

sum2=a[i];
max=a[j];
}
else sum2=a[j];
for(k=j+1,sum3=0;k<n;k++){
if(a[k]>max){

sum3=sum2+max;
max=a[k];
}
else sum3=sum2+a[k];
if(sum3>=max)
count++;



}}} 
cout<<count<<endl;                                           
}


getch();
return 0;

}

I am getting a runtime error
Please tell me where I am wrong....

wat i am doing is calculating max of the 3 and putting other 2 in sum and comparing these two....

The time given is for all the

salvo @ 4 Jun 2011 01:32 PM
The time given is for all the test cases combined or just for the worst case test case

why the program shows error

ayush1723 @ 14 Jul 2011 01:03 AM
why the program shows error with quicksort ?

as far as i know, time limit

cyberax @ 12 Oct 2011 04:25 AM
as far as i know, time limit is unreachable in Python. http://www.codechef.com/viewsolution/700638 Python users, don't loose your time. :)

http://www.codechef.com/views

kapilagarwal @ 26 Feb 2012 09:37 AM
http://www.codechef.com/viewsolution/867333 @admin : pls check this out, it is solving every test case on my comp

http://www.codechef.com/views

karthikthehun @ 15 Mar 2012 04:06 PM
http://www.codechef.com/viewsolution/915161 The program is working in my gcc compiler but is giving a runtime error in codechef.Pls help.

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold

HELP

Program should read from standard input and write to standard output. After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:

 

  • Accepted Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark.
  • Time Limit Exceeded Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach.
  • Wrong Answer Your program compiled and ran succesfully but the output did not match the expected output.
  • Runtime Error Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section.
  • Compilation Error Your code was unable to compile. When you see this icon, click on it for more information.
  • If you are still having problems, see a sample solution here.

CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com