What's missing here? Question : WARRIORS;EASY

#include <stdio.h>
int main(void) {
long long int t,n,q,i,j,temp;
int count=0;
long long int p[1000000],x[1000000];
scanf("%ld",&t);
scanf("%ld %ld",&n,&q);
for(i=0;i<n;i++)
scanf("%ld",&p[i]);
for(i=0;i<q;i++)
scanf("%ld \n",&x[i]);
for (i = 0; i < n; i++) // sort in increasing order, enemy powers
{
for (j = i + 1; j < n; j++)
{
if(p[i] > p[j])
{
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
for(i=0;i<q;i++)
{
for(j=0;j<n;j++)
{
if (x[i]>p[j])
{
count++;
x[i]=2*(x[i]-p[j]);
}
}
printf("%d \n",count);
count=0;
}
return 0;
}

1 Like

Bro please send a link to solution. Without syntax formatting it becomes impossible to read… It’s better if you post a link and tell your appoach as well :smile:

sorry…first timer:sweat_smile:
https://www.codechef.com/viewsolution/26015579

I firstly sorted the enemy powers in ascending order, then straight away applied if else with the conditions.

https://www.codechef.com/viewsolution/26000058
Can anyone point out what is wrong with my code ?

Most certainly x[i] overflows.

Your variable mypower overflows

How do I correct it so that mypower does not overflow ?

How do I correct the overflow?
Also, the code is working fine for a number of test cases.

Most certainly the overflow has been corrected here -CodeChef: Practical coding for everyone

Could you please verify?

Or maybe this
https://www.codechef.com/viewsolution/26020516

One way to deal with the overflows: If mypower at any point is bigger than 210^9 then you will win the next battle and the resulting mypower will be 2(mypower - p[i]) > 210^9. Thus you will finish in a better situation than you started. Therefore if at any point you reach a power bigger than 2*10^9 you win the rest of the battles and you don’t need to iterate further.

In any case your approach will TLE for many reasons (crude sort to begin with). You can take a look at any of the accepted codes:
CodeChef: Practical coding for everyone
to get a faster approach.

2 Likes

(CodeChef: Practical coding for everyone)

I’ve been trying to debug this for a while. Can you please help me find the issue.strong text