REMONE - WA for Test Case -1

With respect to the wording of the question, my logic seems correct, can anyone help me where and why this fails?

#include using namespace std; typedef long long ll; #define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);

int main(){
fast;
ll t;
cin>>t;

while(t--){
	ll n;
	cin>>n;
	vector<ll> a(n,0);
	vector<ll> b(n-1,0);
	ll asum=0,bsum=0;
	for(ll i=0;i<n;i++){
		cin>>a[i];
		asum+=a[i];
	}

	for(ll i=0;i<n-1;i++){
		cin>>b[i];
		bsum+=b[i];
	}
	ll res=1e12;
	for(ll i=0;i<n;i++){
		ll nmo_sum=asum-a[i];
		ll db=n-1;
		ll rem_sum=bsum-nmo_sum;
		
		if((rem_sum)%db==0 && (rem_sum/db>0) ){
			res=min(res,rem_sum/db);
		}
	}
	
	cout<<res<<endl;
}
return 0;

}

hey bro… I think my logic nd ur logic are same … i also need help…whats the wrong in this logic

import java.util.;
import java.io.
;
class MyClass {
public static int mod= 1000000007;
public static int[] merge(int[] a,int[] b) {
int[] m = new int[a.length+b.length];
int i=0,j=0,k=0;
while(i<a.length && j<b.length) {
if(a[i]<b[j]) {
m[k]=a[i];
i++;
} else {
m[k]=b[j];
j++;
}
k++;
}
while(i<a.length) {
m[k]=a[i];
i++;
k++;
}
while(j<b.length) {
m[k]=b[j];
j++;
k++;
}
return m;
}
public static int[] sort(int[] a,int lo,int hi) {
if(lo>=hi) {
int[] ba = new int[1];
ba[0]=a[hi];
return ba;
}
int mi = (lo+hi)/2;
int[] fh = sort(a,lo,mi);
int[] sh = sort(a,mi+1,hi);
int[] fa = merge(fh,sh);
return fa;
}
public static int search(int[] a,int n) {
int lo = 0;
int hi = a.length-1;
while(lo<=hi) {
int mi = (lo+hi)/2;
if(a[mi]==n) {
return mi;
} else if(a[mi]<n) {
lo=mi+1;
} else {
hi=mi-1;
}
}
if(a[hi]!=n) {
hi++;
}
return hi;
}

public static long diff(long a,long b) {
    return a-b;
}
public static long add(long a,long b) {
    return a+b;
}
public static long rem(long a,long b) {
    return a%b;
}
public static long div(long a,long b) {
    return a/b;
}
public static void main(String args[]) throws java.lang.Exception {
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  int t= Integer.parseInt(br.readLine());
  while(t-->0) {
      String[] s1 = br.readLine().split(" ");
      int n = Integer.parseInt(s1[0]);
      String[] s2 = br.readLine().split(" ");
      String[] s3 = br.readLine().split(" ");
      int[] a = new int[n];
      int[] b = new int[n-1];
      long soa =0;
      long sob =0;
      for(int i=0;i<n;i++) {
          a[i]=Integer.parseInt(s2[i]);
          soa = add(soa,a[i]);
        //   soa = (long)(soa+a[i]);
          if(i<n-1) {
              b[i]=Integer.parseInt(s3[i]);
              sob = add(sob,b[i]);
            //   sob = (long)(sob+b[i]);
          }
      }
      long min = Long.MAX_VALUE;
      for(int i=0;i<n;i++) {
          long soe = diff(soa,a[i]);
          long r = rem(diff(sob,soe),n-1);
        //   long soe = (long)(soa-a[i]);
        //   long r = (long)(sob-soe)%(n-1);
          if(r==0) {
              long q = div(diff(sob,soe),n-1);
            //   long q = (long)(sob-soe)/(n-1);
              if(min>=q && q>0) {
                  min=q;
              }
          }
      }
      System.out.println(min);
  }
}

}

I have same problem with this task, I don’t know how, but founded answer doesn’t give correct answer if will sub it from B array (b[i] not found in A while I searching it). In that case I found nearest number from A and add difference to X and I got AC

n = int(input())

for i in range(n):
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    s_a = sum(a)
    s_b = sum(b)
    s_diff = s_b - s_a
    x = 5 * 10 ** 9 + 1
    for j in range(n):
        s_curr = s_diff + a[j]
        if s_curr > 0 and s_curr % (n - 1) == 0:
            x_1 = s_curr // (n - 1)
            x = min(x, x_1)
    a = set(a)
    for i in range(n - 1):
        b[i] -= x
        if b[i] not in a:
            closest = min(a, key=lambda g: abs(g - b[i]))
            x += (closest - b[i])
    print(x)

I too did the exact same thing and got a WA, I am also trying to figure out the fault.