My issue
let’s say we ignored Ai; lets say sum of all elements in A as a, and B as b respectively. a-Ai gives sum of all elements except Ai; now add x to each element(i.e. n-1 times) so now the sum becomes (a-Ai)+ (n-1)X = b; so X= (b-a+A[i])/(n-1). For this to be possible, we must ensure b-a+A[i] is divisible by n-1. we have to find the least value possible for x by iteration. But for some reason my approach is giving wrong answer. Some one please explain.
My code
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
// your code goes here
int t;cin>>t;
while(t--){
int n; ll a=0; ll b=0;int min=INT_MAX;
cin>>n;
vector<int>A;
for(int i=0;i<n;i++){ int x;cin>>x;a+=x;A.push_back(x); }
for(int i=0;i<n-1;i++){int x;cin>>x;b+=x; }
// sort(A.begin(),A.end());
for(int i=0;i<n;i++){
int d= b-a+A[i];
if(d%(n-1) == 0 && d>0){
if(min>d/(n-1)){min=d/(n-1);}
}
}
cout<<min<<endl;
}
return 0;
}
Learning course: Jump from 2* to 3*
Problem Link: CodeChef: Practical coding for everyone