Problem : UWCOI20B

My code : Click here

I am getting TLE in the third subtask. How to avoid/correct this?

Problem : UWCOI20B

My code : Click here

I am getting TLE in the third subtask. How to avoid/correct this?

Why are u using two loops for checking even and odd

Just use

For(i=0;i<N-2;i++)

{ If(a[i]+a[n-1]%2==1)

Count++;

}

Cout<<count;

Ans also put condition before loop for n=1 and n=2

@venkat_asuri if you understand the problem statement we need to find out the no. of pairs such that their sum is odd.

And we know,

even + even = even

odd + odd = even

odd + even = odd

So what we need to do is to count the total no. of odds in the array and total no. of evens and then multiply them to get the answer.

Answer = (Total odds in arr)*(Total evens in arr)

1 Like

main()

{

int T;

scanf("%d",&T);

while(T–){

int n;

scanf("%d",&n);

long long int a[n],i,o=0,e=0;

for(i=0;i<n;i++)

scanf("%lld",&a[i]);

for(i=0;i<n;i++)

{

if(a[i]%2==0)

{

e++;

}

else

o++;

}

printf("%lld\n",(o*e));

}

}

Here is the solution… Solution: 40721945 | CodeChef

In this question we can do it in two ways -

i) using two for loop which will give us TLE

ii) Using one for loop. In this we will count number of even and odd number in the array and use permutations and odd number property.

Odd number property - even + odd = odd

so we have to choose one even and one odd number we use premutation = number of even * number of odd