I got the mistake you are doing here.
(sum(b)-(sum(a)-a[i]))/(n-1);
Here, a[i] is the element which you are trying to remove one by one from the array A to get the array B , but we are not allowed to do this as the second array is already fixed and hence a[i] is also some fixed element.
Hopefully, I had solved your doubt, Please ping me if I didn’t.
You should get different answers from these two cases:
2
8
13 1 3 7 11 17 15 5
7 17 19 15 5 9 13
8
3 13 5 15 17 7 11 1
19 7 11 15 9 5 17
(the A values are the same each time, and the two sets of B numbers have the same min and max)
from whatever i could decipher we have two unknowns in the eqn:
sum(b)=sum(a)-a[i]+(len(a)-1)* x
the two unkowns are a[i] and x.
to solve for x:
x={sum(b)-sum(a)+a[i]}/{len(a)-1}
we are fixing the value of x by varying a[i].It is very much possible that we get an x for such a[i] which might not even be the right answer!
Hence we must backtrack and check if the value of x in valid or not!
I think the issue lies in the fact that our logic works with the diff in sum of A and B. One problem here is that when we are checking
if diff%(n-1) ==0 is that it is not checking if the sum(B) is evenly distributed with X or not
say B[0] is result of addition of X-1 and B[1] is result of X+1 so by our approach we will get this X as answer but it is WA.
t=int(input())
def maxi(d):
m=0
for i in d:
if d[i]>m:
m=d[i]
key=i
else:
pass
return(key)
def check(a,b):
dic={}
for i in a:
for j in b:
diff=abs(i-j)
if diff not in dic:
dic[diff]=1
else:
dic[diff]+=1
return(maxi(dic))
for _ in range(t):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
if n==2:
if b[0]-a[0]<b[-1]-a[-1]:
print(b[-1]-a[-1])
else:
print(b[0]-a[0])
else:
print(check(a,b))
Can anyone tell me what is wrong in my code? I’m getting TLE error.