Remove One Element

#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
int t;
cin>>t;
while(t–){
int n;
cin>>n;
int arrA[n],arrB[n-1];
for(int i=0;i<n;i++)cin>>arrA[i];
for(int i=0;i<n-1;i++)cin>>arrB[i];
int ASum=0,BSum=0;
for(int i=0;i<n;i++)ASum+=arrA[i];
for(int i=0;i<n-1;i++)BSum+=arrB[i];
int ans=-1;
for(int i=0;i<n;i++){
int rhs=(ASum-arrA[i]);
int lhs=(BSum-rhs);
int nMinus1=n-1;
if(lhs>0&&(lhs%nMinus1)==0){
int x=(lhs/nMinus1);
if(BSum-(n-1)*x==ASum-arrA[i]){
if(ans==-1)ans=x;
else ans=min(ans,x);
}
}
}
cout<<ans<<"\n";
}
return 0;
}

i dont know whats wrong in here can anyone help…
link Contest Page | CodeChef

1
3
1 10 12
13 14

There is no answer but your program outputs 7. ik there is always an answer but (BSum-(n-1)*x==ASum-arrA[i]) will not give the correct answer in case there is.

can u please ellaborate , the test case u mentioned above what’s the correct answer for that test case

EDIT : Oh ok got it , thanks a lot

yeah i uderstood the issue but how did u think about it??

ok I have got the proof , so understand this

Intitial array : A[ ]: {a,b,c,d,e}
Array B [ ] : { a+x,b+x,c+x,d+x}

so clearly e was not choosen

so lets say when we are applying our algorithm by excluding a suppose

then the difference in sum is (a+x+b+x+c+x+d+x)-(b+c+d+e) = 4x-e
now this difference must be divisble by 4 and we know that e is not a part so 4
x-e should not be divisible by 4

But what if e is divisble by 4 , → then leads to wrong answer, based on this anti test cases can be generated

this is the proof , I should have done the mathematical analysis :frowning:

1 Like