What is wrong with my code in FFL?

This is the link to my solution. What is wrong with it? CodeChef: Practical coding for everyone

you ignored the case when there is NO attacker or NO defender

#include<bits/stdc++.h>
using namespace std;
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while(t–)
{
int n,s;
cin >> n >> s;
int a[n];
int b[n];
for(int i=0;i<n;i++)
cin >> a[i];
for(int i=0;i<n;i++)
cin >>b[i];
int min=INT_MAX;
int min2=INT_MAX;
for(int i=0;i<n;i++)
{
if(b[i]==0&&a[i]<min)
min=a[i];
else if(b[i]==1&&a[i]<min2)
min2=a[i];
}

if((min+min2)+s<=100)
cout<<“yes”<<endl;
else cout << “”<<endl;

}
}

whats wrong with this??

if(remain==0){
  cout<<"no\n";
 continue;
}

put this code at bottom. Because when remain = 0 you are skipping inputs, which gets considered for next testcase.
I edited your code and checked it got accepted.

https://www.codechef.com/viewsolution/32281012

2 Likes

Integer overflow is causing the error.
INT_MAX + any value will cause an overflow when you are checking for yes/no.

But doesn’t the constraint say that: 1≤N≤100

So there must be at least one attacker or at least one defender.

Ohhh ok. That makes sense. Thanks

Your code gives wrong answer for N=1. Your output is “yes” it should be “no”.

Not just for n = 1, the main reason for wrong answer is that if there is no defender/forward player then the min value would be INT_MAX and when we add it with another int value it results in an overflow.

You didn’t include the possibility if there are only defenders or only forwarders, like if the array contains all 0s and no 1s or vice versa.

N>=1 according to constraints. so that case would not be a problem. But yea overflow is also another problem in code.

Hey, doesn’t matter, you’re saying N>=1, OK fine! But the point is for example when N=5 and all of the 5 positions have forwards in it i.e. 1. Then check what your code gonna do and tell.

but after all sum will be greater than 100 if there only def or only attack?

i tried it but wa

@kshit_happens
It’s due to Integer overflow.
Consider case when there are only defender or forward player. Now your one of the min or min2 will have value equal to INT_MAX. so when your are adding (min+min2)+s it get’s overflow.

One solution is to initialise min and min2 to 101, since max P is 100 according to constraint.

You have misunderstood what I said.
There must be both atleast one attacker and atleast one defender. Having simply an attacker or a defender does not work.