Hello, I’m new in Competitive programming and Math stuff. I was trying to solve this problem and I think my approach is correct.
My Approach:-
Given 2 Arrays A of N size and B of N-1 size and Bi = Ai + X excluding one element that makes B array of size N-1.
They want us to find Smallest X.
So want I did is Find The sum of A like
ASum = sum(A[i to N]);
BSum = sum(B[i to N-1]);
as we can deduct that BSum = A[i to N] (excluding 1 element from A Array) + (N-1) * X;
so I did this for every element of A Array:-
int num = BSum - ( ASum - A[i])
if ( num % (N-1) == 0 && num / (N-1) > 0) min = min(num/(N-1), min);because there is only one element that is excluded from the B Array.
if we get that element then X = BSum - (ASum - A[i](That Excluded Element)) / N-1;
My Code Implementation:-
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int tc = Integer.parseInt(reader.readLine());
while (tc-->0) {
int n = Integer.parseInt(reader.readLine());
String[] aSArr = reader.readLine().split(" ");
String[] bSArr = reader.readLine().split(" ");
long[] A = new long[n];
long ASum = 0, BSum = 0;
for (int i = 0; i < n; i++) {
A[i] = Long.parseLong(aSArr[i]);
ASum += A[i];
}
for (int i = 0; i < n-1; i++) {
BSum += Long.parseLong(bSArr[i]);
}
int min = Integer.MAX_VALUE;
int b = n-1;
for (int i = 0; i < n; i++) {
long num = BSum - (ASum - A[i]);
if (num % b == 0 && num / b > 0) {
min = Math.min((int)num/b, min);
}
}
System.out.println(min);
}
}
}