little chef and numbers(march challenge )

#include
using namespace std;

int main()
{
int t,i,j,k,l,n,m,o;
cin>>t;
for(i=0;i<t;i++)
{
	cin>>n;
	k=0;l=0;
	for(j=0;j<n;j++)
	{
		cin>>m;
		if(m==0 || m==1)
		k++;
		if(m==2)
		l++;
	}
	if(l>1)
{	o=n-k-l;
	o=o*(o-1)/2;
	o=o+(l*o);
	cout<<o<<endl;
}
	else
	{
		o=n-k;
	o=o*(o-1)/2;
	cout<<o<<endl;
	}
 }
}

plz find the error

It doesn’t work like this, simply throwing out the code and asking to find the error. You could have simply given the link to your submission, for that matter. You are expected to tell what you tried, and what is your question accordingly.

However the idea was to choose 2 numbers from all numbers, and subtract the combinations that you can get when you choose 2 numbers from all such numbers whose magnitude is 2. So, if gt represents number of numbers greater than 2, and t represents number of numbers equal to 2, then (gt + t) C 2 - t C 2 will be the answer, i.e. e*(e-1)/2 - t*(t-1)/2 where e = gt + t

1 Like

Integer overflow on target (32 bit) architecture.

I tested your code on most test simple test cases and it worked fine. The mistake in your code is that the value of n can be upto 10^5 so n * (n-1 ) will be around 10^10 which will exceed the range of int data type. I would suggest you replace int by long long and submit.

P.S. Using o as a variable is a heinous crime :smiley: !! It becomes very difficult to differentiate between 0 and o making debugging hard.

Agreed, the question is “incomlete” at least…

@groovypalak: is the TLE not WA the problem? Read the forum for fast I/O…

@v_akshay >> reason for downvoting?

That submission gives TLE. Although you are right about overflow. http://www.codechef.com/viewsolution/3617616

Attaching the most recent submission of the OP: http://www.codechef.com/viewsolution/3588398

Can you retry writing std::ios_base::sync_with_stdio(false) as the first line in main. There are 3 major reasons for time out I have encountered. Incorrect condition in loop (for, while), non-converging condition in some special case of recursion and slow IO. My suggestion would try to handle third case that I suspect.

Also now that you have said this submission gave TLE, replace the cin and cout by printf and scanf statements.

Initially your answer wasn’t complete IMO, Now it is so take an upvote and be happy :slight_smile: